1. Go to this page and download the library: Download react/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/ */
$promise
->catch(function (\RuntimeException $reason) {
// Only catch \RuntimeException instances
// All other types of errors will propagate automatically
})
->catch(function (\Throwable $reason) {
// Catch other errors
});
$resolver = function (callable $resolve, callable $reject) {
// Do some work, possibly asynchronously, and then
// resolve or reject.
$resolve($awesomeResult);
// or throw new Exception('Promise rejected');
// or $resolve($anotherPromise);
// or $reject($nastyError);
};
$canceller = function () {
// Cancel/abort any running operations like network connections, streams etc.
// Reject promise by throwing an exception
throw new Exception('Promise cancelled');
};
$promise = new React\Promise\Promise($resolver, $canceller);
function incorrect(): int
{
$promise = React\Promise\reject(new RuntimeException('Request failed'));
// Commented out: No rejection handler registered here.
// $promise->then(null, function (\Throwable $e): void { /* ignore */ });
// Returning from a function will remove all local variable references, hence why
// this will report an unhandled promise rejection here.
return 42;
}
// Calling this function will log an error message plus its stack trace:
// Unhandled promise rejection with RuntimeException: Request failed in example.php:10
incorrect();
// Unhandled promise rejection with RuntimeException: Unhandled in example.php:2
React\Promise\reject(new RuntimeException('Unhandled'));
function getAwesomeResultPromise()
{
$deferred = new React\Promise\Deferred();
// Execute a Node.js-style function using the callback pattern
computeAwesomeResultAsynchronously(function (\Throwable $error, $result) use ($deferred) {
if ($error) {
$deferred->reject($error);
} else {
$deferred->resolve($result);
}
});
// Return the promise
return $deferred->promise();
}
getAwesomeResultPromise()
->then(
function ($value) {
// Deferred resolved, do something with $value
},
function (\Throwable $reason) {
// Deferred rejected, do something with $reason
}
);
$deferred = new React\Promise\Deferred();
$deferred->promise()
->then(function ($x) {
// $x will be the value passed to $deferred->resolve() below
// and returns a *new promise* for $x + 1
return $x + 1;
})
->then(function ($x) {
// $x === 2
// This handler receives the return value of the
// previous handler.
return $x + 1;
})
->then(function ($x) {
// $x === 3
// This handler receives the return value of the
// previous handler.
return $x + 1;
})
->then(function ($x) {
// $x === 4
// This handler receives the return value of the
// previous handler.
echo 'Resolve ' . $x;
});
$deferred->resolve(1); // Prints "Resolve 4"
$deferred = new React\Promise\Deferred();
$deferred->promise()
->then(function ($x) {
throw new \Exception($x + 1);
})
->catch(function (\Exception $x) {
// Propagate the rejection
throw $x;
})
->catch(function (\Exception $x) {
// Can also propagate by returning another rejection
return React\Promise\reject(
new \Exception($x->getMessage() + 1)
);
})
->catch(function ($x) {
echo 'Reject ' . $x->getMessage(); // 3
});
$deferred->resolve(1); // Prints "Reject 3"
$deferred = new React\Promise\Deferred();
$deferred->promise()
->then(function ($x) {
return $x + 1;
})
->then(function ($x) {
throw new \Exception($x + 1);
})
->catch(function (\Exception $x) {
// Handle the rejection, and don't propagate.
// This is like catch without a rethrow
return $x->getMessage() + 1;
})
->then(function ($x) {
echo 'Mixed ' . $x; // 4
});
$deferred->resolve(1); // Prints "Mixed 4"
php
$promise->cancel();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.