PHP code example of agussuroyo / async

1. Go to this page and download the library: Download agussuroyo/async library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

agussuroyo / async example snippets


use AgusSuroyo\Async\Async;

$async = new Async();

$future = $async->run(function () {
    return fetchPrice('SKU-1');
});

$price = $future->await(); // blocks until the child finishes

$async = new Async();

$futures = [];
foreach ($skus as $sku) {
    $futures[$sku] = $async->run(function () use ($sku) {
        return fetchPrice($sku);
    });
}

$prices = array_map(function ($f) { return $f->await(); }, $futures);

$future = $async->run(function () {
    throw new \RuntimeException('boom');
});

try {
    $future->await();
} catch (\RuntimeException $e) {
    // 'boom' — child file/line/trace are appended to the message
}

$async = new Async(4);  // at most 4 children at once
$async->max(8);         // change later

foreach ($items as $item) {
    $async->run(function () use ($item) {
        process($item);
    });
}
$async->wait(); // blocks until every scheduled child has finished
sh
docker compose up -d
docker compose exec php composer test
docker compose exec php composer analyse