-
-
Save oleksandr-roskovynskyi/d9c258070385404a521fbdaba6694106 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use GuzzleHttp\Promise\EachPromise; | |
use GuzzleHttp\Psr7\Response; | |
$users = ['one', 'two', 'three']; | |
$promises = (function () use ($users) { | |
foreach ($users as $user) { | |
// don't forget using generator | |
yield $this->getAsync('https://api.demo.com/v1/users?username=' . $user); | |
} | |
})(); | |
$eachPromise = new EachPromise($promises, [ | |
// how many concurrency we are use | |
'concurrency' => 4, | |
'fulfilled' => function (Response $response) { | |
if ($response->getStatusCode() == 200) { | |
$user = json_decode($response->getBody(), true); | |
// processing response of user here | |
} | |
}, | |
'rejected' => function ($reason) { | |
// handle promise rejected here | |
} | |
]); | |
$eachPromise->promise()->wait(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment