Lines Matching refs:promise
3 [Promises/A+](https://promisesaplus.com/) implementation that handles promise
4 chaining and resolution iteratively, allowing for "infinite" promise chaining
13 - [Promise](#promise)
16 - [Promise interop](#promise-interop)
24 "infinite" promise chaining.
49 A *promise* represents the eventual result of an asynchronous operation. The
50 primary way of interacting with a promise is through its `then` method, which
51 registers callbacks to receive either a promise's eventual value or the reason
52 why the promise cannot be fulfilled.
63 $promise = new Promise();
64 $promise->then(
67 echo 'The promise was fulfilled.';
71 echo 'The promise was rejected.';
76 *Resolving* a promise means that you either fulfill a promise with a *value* or
77 reject a promise with a *reason*. Resolving a promise triggers callbacks
78 registered with the promise's `then` method. These callbacks are triggered
83 Promises are fulfilled using the `resolve($value)` method. Resolving a promise
85 all of the onFulfilled callbacks (resolving a promise with a rejected promise
86 will reject the promise and trigger the `$onRejected` callbacks).
91 $promise = new Promise();
92 $promise
103 // Resolving the promise triggers the $onFulfilled callbacks and outputs
105 $promise->resolve('reader.');
111 promise. The return value of a promise is what's forwarded to the next
112 promise in the chain. Returning a promise in a `then` callback will cause the
113 subsequent promises in the chain to only be fulfilled when the returned promise
114 has been fulfilled. The next promise in the chain will be invoked with the
115 resolved value of the promise.
120 $promise = new Promise();
123 $promise
133 $promise->resolve('A');
140 When a promise is rejected, the `$onRejected` callbacks are invoked with the
146 $promise = new Promise();
147 $promise->then(null, function ($reason) {
151 $promise->reject('Error!');
163 $promise = new Promise();
164 $promise->then(null, function ($reason) {
170 $promise->reject('Error!');
173 You can also forward a rejection down the promise chain by returning a
181 $promise = new Promise();
182 $promise->then(null, function ($reason) {
188 $promise->reject('Error!');
192 does not return a rejected promise, downstream `$onFulfilled` callbacks are
198 $promise = new Promise();
199 $promise
207 $promise->reject('Error!');
213 You can synchronously force promises to complete using a promise's `wait`
214 method. When creating a promise, you can provide a wait function that is used
215 to synchronously force a promise to complete. When a wait function is invoked
216 it is expected to deliver a value to the promise or reject the promise. If the
218 function provided to a promise constructor is invoked when the `wait` function
219 of the promise is called.
222 $promise = new Promise(function () use (&$promise) {
223 $promise->resolve('foo');
226 // Calling wait will return the value of the promise.
227 echo $promise->wait(); // outputs "foo"
230 If an exception is encountered while invoking the wait function of a promise,
231 the promise is rejected with the exception and the exception is thrown.
234 $promise = new Promise(function () use (&$promise) {
238 $promise->wait(); // throws the exception.
241 Calling `wait` on a promise that has been fulfilled will not trigger the wait
245 $promise = new Promise(function () { die('this is not called!'); });
246 $promise->resolve('foo');
247 echo $promise->wait(); // outputs "foo"
250 Calling `wait` on a promise that has been rejected will throw an exception. If
256 $promise = new Promise();
257 $promise->reject('foo');
258 $promise->wait();
261 …ught exception 'GuzzleHttp\Promise\RejectionException' with message 'The promise was rejected with…
265 When synchronously waiting on a promise, you are joining the state of the
266 promise into the current state of execution (i.e., return the value of the
267 promise if it was fulfilled or throw an exception if it was rejected). This is
268 called "unwrapping" the promise. Waiting on a promise will by default unwrap
269 the promise state.
271 You can force a promise to resolve and *not* unwrap the state of the promise
275 $promise = new Promise();
276 $promise->reject('foo');
277 // This will not throw an exception. It simply ensures the promise has
279 $promise->wait(false);
282 When unwrapping a promise, the resolved value of the promise will be waited
283 upon until the unwrapped value is not a promise. This means that if you resolve
284 promise A with a promise B and unwrap promise A, the value returned by the
285 wait function will be the value delivered to promise B.
287 **Note**: when you do not unwrap the promise, no value is returned.
292 You can cancel a promise that has not yet been fulfilled using the `cancel()`
293 method of a promise. When creating a promise you can provide an optional
295 of the promise.
302 When creating a promise object, you can provide an optional `$waitFn` and
304 expected to resolve the promise. `$cancelFn` is a function with no arguments
305 that is expected to cancel the computation of a promise. It is invoked when the
306 `cancel()` method of a promise is called.
311 $promise = new Promise(
312 function () use (&$promise) {
313 $promise->resolve('waited');
316 // do something that will cancel the promise computation (e.g., close
321 assert('waited' === $promise->wait());
324 A promise has the following methods:
328 …Appends fulfillment and rejection handlers to the promise, and returns a new promise resolving to …
332 …e promise, and returns a new promise resolving to the return value of the callback if it is called…
336 Synchronously waits on the promise to complete.
338 `$unwrap` controls whether or not the value of the promise is returned for a
339 fulfilled promise or if an exception is thrown if the promise is rejected.
344 Attempts to cancel the promise if possible. The promise being cancelled and
346 cancelled. Any promises waiting on the cancelled promise to resolve will also
351 Returns the state of the promise. One of `pending`, `fulfilled`, or
356 Fulfills the promise with the given `$value`.
360 Rejects the promise with the given `$reason`.
365 A fulfilled promise can be created to represent a promise that has been
371 $promise = new FulfilledPromise('value');
374 $promise->then(function ($value) {
382 A rejected promise can be created to represent a promise that has been
388 $promise = new RejectedPromise('Error');
391 $promise->then(null, function ($reason) {
400 you can use Guzzle promises with [React promises](https://github.com/reactphp/promise)
401 for example. When a foreign promise is returned inside of a then method
402 callback, promise resolution will occur recursively.
405 // Create a React promise
407 $reactPromise = $deferred->promise();
409 // Create a Guzzle promise that is fulfilled with a React promise.
413 // Return the React promise
419 a foreign promise. You will need to wrap a third-party promise with a Guzzle
420 promise in order to utilize wait and cancel functions with foreign promises.
427 task queue will be automatically run to ensure that the blocking promise and
478 When a promise is fulfilled or rejected with a non-promise value, the promise
479 then takes ownership of the handlers of each child promise and delivers values
482 When a promise is resolved with another promise, the original promise transfers
483 all of its pending handlers to the new promise. When the new promise is
489 Some promise libraries implement promises using a deferred object to represent
490 a computation and a promise object to represent the delivery of the result of
492 consumers of the promise cannot modify the value that will be eventually
495 One side effect of being able to implement promise resolution and chaining
496 iteratively is that you need to be able for one promise to reach into the state
497 of another promise to shuffle around ownership of handlers. In order to achieve
498 this without making the handlers of a promise publicly mutable, a promise is
505 $promise = new Promise();
506 $promise->then(function ($value) { echo $value; });
507 // The promise is the deferred value, so you can deliver a value to it.
508 $promise->resolve('foo');