tleckie/async

1.0.6 2021-04-25 10:59 UTC

This package is auto-updated.

Last update: 2022-06-11 09:41:28 UTC


README

Latest Version on Packagist Scrutinizer Code Quality Build Status Total Downloads

Installation

You can install the package via composer:

composer require tleckie/async

Usage

<?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();

Catch exception

<?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();