Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Implement async call in PHP using guzzle promise
use GuzzleHttp\Promise\Promise;
use GuzzleHttp\Promise\EachPromise;
function promiseFunction($sample_array,$index){ // index is optional params
$promise = new Promise(
function () use ($sample_array,$index,&$promise) {
$response = secondFunction($sample_array);
function ($sample_array,$index,&$promise) {
// do something that will cancel the promise computation (e.g., close
// a socket, cancel a database query, etc...)
$promise->reject($index); // just to know which index has failed or rejected.
return $promise;
function sampleFunction($sample_array_set){
$promises = array();
foreach($sample_array_set as $index => $sample_array){
$promises[$index] = promiseFunction($sample_array,$index);
$eachPromise = new EachPromise($promises, [
// how many concurrency we are use
'concurrency' => count($promises), // can set to 4 ,10,20 depend on how many iteration but better to used same as count
'fulfilled' => function ($response,$index) use (&$sample_array)
//logic for other operation.
'rejected' => function ($reason) {
$sample_array_set = array('One','Two','Three', 'Four', 'Five', 'Six', 'Seven', 'Eight');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment