react/promise
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
React/Promise is a PHP library providing an implementation of CommonJS Promises/A. Essentially, it helps to manage and coordinate the flow of asynchronous operations, making it a great tool for handling actions such as API calls or database operations that are not instantaneous. This library can make your code easier to read and maintain by providing a robust, canonical way to work with asynchronous operations.
Using React/Promise is fairly straightforward. After installation via Composer, you can start utilizing this library in your PHP codes. Here's an example of a function returning a deferred promise:
function getAsyncValue() {
$deferred = new \React\Promise\Deferred();
someAsynchronousFunction(function($error, $result) {
if ($error) {
$deferred->reject($error);
} else {
$deferred->resolve($result);
}
});
return $deferred->promise();
}
getAsyncValue()->then(
function ($value) {
echo 'Value: ' . $value;
},
function ($error) {
echo 'Error: ' . $error;
}
);
In this example, getAsyncValue()
performs an asynchronous operation and returns a promise for its result. We attach fulfillment and rejection handlers to it using then()
. The fulfillment handler will be executed with the result upon success, while the rejection handler will be executed with an error upon failure.
You can find the full documentation of React/Promise on the library's Github page at https://github.com/reactphp/promise. It includes explanations of various concepts and APIs involved such as Deferred
, Promise
, resolve()
, reject()
, and so on. It also features a number of operation patterns you may come across when dealing with promises. From basic usage to detailed examples and scenarios, the documentation is a complete resource for understanding and effectively using the React/Promise library.