PHP code example of tsiura / promise-queue

1. Go to this page and download the library: Download tsiura/promise-queue 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/ */

    

tsiura / promise-queue example snippets


$q = new Queue();

$d1 = new Deferred();
$q->execute(function (int $jobId) use ($d1) {
    echo 'Run first' . PHP_EOL;
    return $d1->promise();
})->then(function ($value) {
    echo 'Resolve first: ' . $value . PHP_EOL;
});

$d2 = new Deferred();
$q->execute(function (int $jobId) use ($d2) {
    echo 'Run second' . PHP_EOL;
    return $d2->promise();
})->then(function ($value) {
    echo 'Resolve second: ' . $value . PHP_EOL;
});

$d3 = new Deferred();
$q->execute(function (int $jobId) use ($d3) {
    echo 'Run third' . PHP_EOL;
    return $d3->promise();
})->then(function ($value) {
    echo 'Resolve third: ' . $value . PHP_EOL;
});

$d1->resolve('value1');
$d2->resolve('value2');
$d3->resolve('value3');

$d1->resolve('value1');
$d3->resolve('value3');
$d2->resolve('value2');

$q->execute(function (int $jobId) use ($d2) {
    echo 'Runing job' . $jobId . PHP_EOL;
    return 1000;
})->then(function ($value) {
    echo 'Resolve job value: ' . $value . PHP_EOL;
});