Download the PHP package guzzlehttp/promises without Composer
On this page you can find all versions of the php package guzzlehttp/promises. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
More information about guzzlehttp/promises
Files in guzzlehttp/promises
Informations about the package promises
Promises/A+ implementation that handles promise chaining and resolution iteratively, allowing for "infinite" promise chaining while keeping the stack size constant. Read this blog post for a general introduction to promises.
- Quick start
- Synchronous wait
- Promise interop
- Implementation notes
- Promises/A+ implementation.
- Promise resolution and chaining is handled iteratively, allowing for "infinite" promise chaining.
- Promises have a synchronous
- Promises can be cancelled.
- Works with any object that has a
- C# style async/await coroutine promises using
|1.x||Bug and security fixes||>=5.5,<8.3|
A promise represents the eventual result of an asynchronous operation. The
primary way of interacting with a promise is through its
then method, which
registers callbacks to receive either a promise's eventual value or the reason
why the promise cannot be fulfilled.
Callbacks are registered with the
then method by providing an optional
$onFulfilled followed by an optional
Resolving a promise means that you either fulfill a promise with a value or
reject a promise with a reason. Resolving a promise triggers callbacks
registered with the promise's
then method. These callbacks are triggered
only once and in the order in which they were added.
Resolving a Promise
Promises are fulfilled using the
resolve($value) method. Resolving a promise
with any value other than a
GuzzleHttp\Promise\RejectedPromise will trigger
all of the onFulfilled callbacks (resolving a promise with a rejected promise
will reject the promise and trigger the
Promises can be chained one after the other. Each then in the chain is a new
promise. The return value of a promise is what's forwarded to the next
promise in the chain. Returning a promise in a
then callback will cause the
subsequent promises in the chain to only be fulfilled when the returned promise
has been fulfilled. The next promise in the chain will be invoked with the
resolved value of the promise.
When a promise is rejected, the
$onRejected callbacks are invoked with the
If an exception is thrown in an
$onRejected callback, subsequent
$onRejected callbacks are invoked with the thrown exception as the reason.
You can also forward a rejection down the promise chain by returning a
GuzzleHttp\Promise\RejectedPromise in either an
If an exception is not thrown in a
$onRejected callback and the callback
does not return a rejected promise, downstream
$onFulfilled callbacks are
invoked using the value returned from the
You can synchronously force promises to complete using a promise's
method. When creating a promise, you can provide a wait function that is used
to synchronously force a promise to complete. When a wait function is invoked
it is expected to deliver a value to the promise or reject the promise. If the
wait function does not deliver a value, then an exception is thrown. The wait
function provided to a promise constructor is invoked when the
of the promise is called.
If an exception is encountered while invoking the wait function of a promise, the promise is rejected with the exception and the exception is thrown.
wait on a promise that has been fulfilled will not trigger the wait
function. It will simply return the previously resolved value.
wait on a promise that has been rejected will throw an exception. If
the rejection reason is an instance of
\Exception the reason is thrown.
GuzzleHttp\Promise\RejectionException is thrown and the reason
can be obtained by calling the
getReason method of the exception.
PHP Fatal error: Uncaught exception 'GuzzleHttp\Promise\RejectionException' with message 'The promise was rejected with value: foo'
Unwrapping a Promise
When synchronously waiting on a promise, you are joining the state of the promise into the current state of execution (i.e., return the value of the promise if it was fulfilled or throw an exception if it was rejected). This is called "unwrapping" the promise. Waiting on a promise will by default unwrap the promise state.
You can force a promise to resolve and not unwrap the state of the promise
false to the first argument of the
When unwrapping a promise, the resolved value of the promise will be waited upon until the unwrapped value is not a promise. This means that if you resolve promise A with a promise B and unwrap promise A, the value returned by the wait function will be the value delivered to promise B.
Note: when you do not unwrap the promise, no value is returned.
You can cancel a promise that has not yet been fulfilled using the
method of a promise. When creating a promise you can provide an optional
cancel function that when invoked cancels the action of computing a resolution
of the promise.
When creating a promise object, you can provide an optional
$waitFn is a function that is invoked with no arguments and is
expected to resolve the promise.
$cancelFn is a function with no arguments
that is expected to cancel the computation of a promise. It is invoked when the
cancel() method of a promise is called.
A promise has the following methods:
then(callable $onFulfilled, callable $onRejected) : PromiseInterface
Appends fulfillment and rejection handlers to the promise, and returns a new promise resolving to the return value of the called handler.
otherwise(callable $onRejected) : PromiseInterface
Appends a rejection handler callback to the promise, and returns a new promise resolving to the return value of the callback if it is called, or to its original fulfillment value if the promise is instead fulfilled.
wait($unwrap = true) : mixed
Synchronously waits on the promise to complete.
$unwrapcontrols whether or not the value of the promise is returned for a fulfilled promise or if an exception is thrown if the promise is rejected. This is set to
Attempts to cancel the promise if possible. The promise being cancelled and the parent most ancestor that has not yet been resolved will also be cancelled. Any promises waiting on the cancelled promise to resolve will also be cancelled.
getState() : string
Returns the state of the promise. One of
Fulfills the promise with the given
Rejects the promise with the given
A fulfilled promise can be created to represent a promise that has been fulfilled.
A rejected promise can be created to represent a promise that has been rejected.
This library works with foreign promises that have a
then method. This means
you can use Guzzle promises with React promises
for example. When a foreign promise is returned inside of a then method
callback, promise resolution will occur recursively.
Please note that wait and cancel chaining is no longer possible when forwarding a foreign promise. You will need to wrap a third-party promise with a Guzzle promise in order to utilize wait and cancel functions with foreign promises.
Event Loop Integration
In order to keep the stack size constant, Guzzle promises are resolved asynchronously using a task queue. When waiting on promises synchronously, the task queue will be automatically run to ensure that the blocking promise and any forwarded promises are resolved. When using promises asynchronously in an event loop, you will need to run the task queue on each tick of the loop. If you do not run the task queue, then promises will not be resolved.
You can run the task queue using the
run() method of the global task queue
For example, you could use Guzzle promises with React using a periodic timer:
Promise Resolution and Chaining is Handled Iteratively
By shuffling pending handlers from one owner to another, promises are resolved iteratively, allowing for "infinite" then chaining.
When a promise is fulfilled or rejected with a non-promise value, the promise then takes ownership of the handlers of each child promise and delivers values down the chain without using recursion.
When a promise is resolved with another promise, the original promise transfers all of its pending handlers to the new promise. When the new promise is eventually resolved, all of the pending handlers are delivered the forwarded value.
A Promise is the Deferred
Some promise libraries implement promises using a deferred object to represent a computation and a promise object to represent the delivery of the result of the computation. This is a nice separation of computation and delivery because consumers of the promise cannot modify the value that will be eventually delivered.
One side effect of being able to implement promise resolution and chaining iteratively is that you need to be able for one promise to reach into the state of another promise to shuffle around ownership of handlers. In order to achieve this without making the handlers of a promise publicly mutable, a promise is also the deferred value, allowing promises of the same parent class to reach into and modify the private properties of promises of the same type. While this does allow consumers of the value to modify the resolution or rejection of the deferred, it is a small price to pay for keeping the stack size constant.
Upgrading from Function API
A static API was first introduced in 1.4.0, in order to mitigate problems with functions conflicting between global and local copies of the package. The function API was removed in 2.0.0. A migration table has been provided here for your convenience:
|Original Function||Replacement Method|
If you discover a security vulnerability within this package, please send an email to [email protected]. All security vulnerabilities will be promptly addressed. Please do not disclose security-related issues publicly until a fix has been announced. Please see Security Policy for more information.
Guzzle is made available under the MIT License (MIT). Please see License File for more information.
Available as part of the Tidelift Subscription
The maintainers of Guzzle and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. Learn more.