Skip to content

teodoroleckie/async

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Asynchronous and parallel PHP

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