PHP code example of ajayvohra2005 / hack-promises

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

    

ajayvohra2005 / hack-promises example snippets


use type HackPromises\Promise;

<<__EntryPoint>>
function promise_rejection_example(): void {
  n(null, (mixed $reason): void ==> {
            $msg = $reason as string;
            echo "{$msg}\n";
        });

    // Outputs "Error!"
    $promise->reject('Error!');
}

use type HackPromises\{Promise, RejectedPromise};

<<__EntryPoint>>
function rejection_forwarding_example(): void {
  ): mixed ==> {
            return new RejectedPromise($reason);
        })
        ->then(null, (mixed $reason): void ==> {
        });

    // Outputs nothing
    $promise->reject('No Error!');
}

use namespace HackPromises as P;

<<__EntryPoint>>
function ignore_rejection_example(): void 
{
  ixed $reason): mixed ==> {
        return "It's ok";
    })->then( (mixed $value): void ==> {
        echo $value as string;
    });

  // Outputs 'It's ok'
  $promise->reject('Error!');
}

use namespace HackPromises as P;

<<__EntryPoint>>
function unwrapping_example(): void 
{
  esolveCallback $cb): void ==> { $cb($b);});
  $b->resolve('foo');
  $result = $a->wait() as string;

  // Outputs 'foo'
  echo $result;
}