0

I tried implementing the below code but it is showing the EachPromise Error class not found and Promise Error class not found. Guzzle library is installed. Then also this error was there.

<?php

use GuzzleHttp\Promise\EachPromise;
use GuzzleHttp\Psr7\Response;

$users = ['one', 'two', 'three'];

$promises = (function () use ($users) {
    foreach ($users as $user) {
        
        // Using generator
        yield $this->getAsync(
'https://api.demo.com/v1/users?username='
        . $user);       
    }
})();

$eachPromise = new EachPromise($promises, [
    
    // Number of concurrency
    'concurrency' => 4,
    'fulfilled' => function (Response $response) {
        if ($response->getStatusCode() == 200) {
            $user = json_decode(
                $response->getBody(), true);
            
            // processing response of the user
        }
    },
    
    'rejected' => function ($reason) {
    // handle promise rejected
    }
]);
$eachPromise->promise()->wait();
?>
jeb
  • 78,592
  • 17
  • 171
  • 225
Sheldon
  • 11
  • 1

1 Answers1

1

Seem there is no autoloader that locates and loads the class files mentioned in your code.

If your Guzzle package is installed with composer, you may try require_once() the /vendor/autoloader.php file in your source root.

Otherwise, try loading those class files individually - which is not recommended in modern PHP world(use autoloader whenever possible).

require_once("path-to/EachPromise.php");
require_once("path-to/Response.php");

EDIT

In the second sight, your code uses $this that requires an object instance context with $this->getAsync() being available, but apparently not so. The code seems like a partially copy-pasted code fragment from a Class object that won't run in your given set up.

You may need to set up the code context of your sample code.

Kita
  • 2,604
  • 19
  • 25
  • After adding the require_once files , facing this error Uncaught Error: Call to undefined function EachPromise() For this code ```$eachPromise = new EachPromise($promises, [ // Number of concurrency 'concurrency' => 4, 'fulfilled' => function (Response $response) { if ($response->getStatusCode() == 200) { $user = json_decode( $response->getBody(), true); // processing response of the user } }, 'rejected' => function ($reason) { // handle promise rejected } ]);``` – Sheldon Aug 13 '21 at 08:06
  • is there any way to implement this particular code – Sheldon Aug 13 '21 at 08:34