PHP code example of gandung / promise

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

    

gandung / promise example snippets


use Gandung\Promise\Deferred;

$deferred = new Deferred();

$deferred->resolve('success.');

$deferred->promise()->then(
	function($d) {
		echo sprintf("%s" . PHP_EOL, $d);
	},
	function($e) {
		echo sprintf("%s" . PHP_EOL, $e);
	}
);

use Gandung\Promise\Deferred;

$deferred = new Deferred();

$deferred->reject('fail.');

$deferred->promise()->then(
	function($d) {
		echo sprintf("%s" . PHP_EOL, $d);
	},
	function($e) {
		echo sprintf("%s" . PHP_EOL, $e);
	}
);

use Gandung\Promise\Promise;

$promise = new Promise();

$promise->then(
	function($d) {
		echo sprintf("%s" . PHP_EOL, $d);
	},
	function($e) {
		throw new \Exception($e);
	}
);

use Gandung\Promise\FulfilledPromise;

$promise = new FulfilledPromise('the quick dirty brown fox.');

$promise->then(function($d) {
	echo sprintf("%s" . PHP_EOL, $d);
});

use Gandung\Promise\RejectedPromise;

$promise = new RejectedPromise('the quick dirty brown fox.');

$promise->then(null, function($e) {
	throw new \Exception($e);
});