PHP code example of ucscode / promise

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

    

ucscode / promise example snippets


use Ucscode\Promise\Promise;

$promise = new Promise(function ($resolve, $reject) {
    // Asynchronous operation
    sleep(20);
    $resolve("Operation Successful");
});

$promise->then(
    fn ($value) => "Fulfilled: $value",
    fn ($reason) => "Rejected: $reason"
)
->finally(fn () => "Operation complete");

$promises = [
    new Promise(function ($resolve) { $resolve(1); }),
    new Promise(function ($resolve) { $resolve(2); }),
    new Promise(function ($resolve) { $resolve(3); }),
];

Promise::all($promises)->then(
    function ($values) {
        // All promises fulfilled
        var_dump($values); // Output: array(1, 2, 3)
    },
    fn ($reason) => "At least one promise rejected"
);