Using Promises in PHP
While using Reactive Extensions, we think of data as continuous streams that emit data over time. A similar, and probably more familiar, concept is Promises, which represent a single value in the future.
You've probably met Promises in libraries such as jQuery, where it's commonly used to handle responses from an AJAX request. There are multiple implementations in PHP, but the principle is always the same. We're going to use a library called reactphp/promise
(
https://github.com/reactphp/promise
), which follows Promises/A proposal (
http://wiki.commonjs.org/wiki/Promises/A
) and adds some extra functionality as well. Since we're going to use this library for this and the next chapter, we will have a look at how to use it.
Install react/promise
package via composer:
$ composer require react/promiseWe
We will use two basic classes:
Promise
: This class represents a result of a deferred computation that will be available in the future.Deferred
: This class represents an...