You can install the package via composer:
composer require tleckie/async
<?php
use Tleckie\Async\Async;
$async = new Async();
foreach([1,2,3,4,5,6,7,8,9,10] as $value){
$async->add(static function() use($value){
sleep(1);
return $value*2;
})->then(static function($value){
var_dump($value);
})->catch(static function(\Exception $exception){
var_dump($exception->getMessage());
});
}
$async->wait();
<?php
use Tleckie\Async\Async;
$async = new Async();
$async->add(static function (){
throw new \Exception('Error...');
})->then(static function ($value) {
})->catch(static function ($exception) {
var_dump($exception);
});
$async->wait();