Chris@0
|
1 # Guzzle Promises
|
Chris@0
|
2
|
Chris@0
|
3 [Promises/A+](https://promisesaplus.com/) implementation that handles promise
|
Chris@0
|
4 chaining and resolution iteratively, allowing for "infinite" promise chaining
|
Chris@0
|
5 while keeping the stack size constant. Read [this blog post](https://blog.domenic.me/youre-missing-the-point-of-promises/)
|
Chris@0
|
6 for a general introduction to promises.
|
Chris@0
|
7
|
Chris@0
|
8 - [Features](#features)
|
Chris@0
|
9 - [Quick start](#quick-start)
|
Chris@0
|
10 - [Synchronous wait](#synchronous-wait)
|
Chris@0
|
11 - [Cancellation](#cancellation)
|
Chris@0
|
12 - [API](#api)
|
Chris@0
|
13 - [Promise](#promise)
|
Chris@0
|
14 - [FulfilledPromise](#fulfilledpromise)
|
Chris@0
|
15 - [RejectedPromise](#rejectedpromise)
|
Chris@0
|
16 - [Promise interop](#promise-interop)
|
Chris@0
|
17 - [Implementation notes](#implementation-notes)
|
Chris@0
|
18
|
Chris@0
|
19
|
Chris@0
|
20 # Features
|
Chris@0
|
21
|
Chris@0
|
22 - [Promises/A+](https://promisesaplus.com/) implementation.
|
Chris@0
|
23 - Promise resolution and chaining is handled iteratively, allowing for
|
Chris@0
|
24 "infinite" promise chaining.
|
Chris@0
|
25 - Promises have a synchronous `wait` method.
|
Chris@0
|
26 - Promises can be cancelled.
|
Chris@0
|
27 - Works with any object that has a `then` function.
|
Chris@0
|
28 - C# style async/await coroutine promises using
|
Chris@0
|
29 `GuzzleHttp\Promise\coroutine()`.
|
Chris@0
|
30
|
Chris@0
|
31
|
Chris@0
|
32 # Quick start
|
Chris@0
|
33
|
Chris@0
|
34 A *promise* represents the eventual result of an asynchronous operation. The
|
Chris@0
|
35 primary way of interacting with a promise is through its `then` method, which
|
Chris@0
|
36 registers callbacks to receive either a promise's eventual value or the reason
|
Chris@0
|
37 why the promise cannot be fulfilled.
|
Chris@0
|
38
|
Chris@0
|
39
|
Chris@0
|
40 ## Callbacks
|
Chris@0
|
41
|
Chris@0
|
42 Callbacks are registered with the `then` method by providing an optional
|
Chris@0
|
43 `$onFulfilled` followed by an optional `$onRejected` function.
|
Chris@0
|
44
|
Chris@0
|
45
|
Chris@0
|
46 ```php
|
Chris@0
|
47 use GuzzleHttp\Promise\Promise;
|
Chris@0
|
48
|
Chris@0
|
49 $promise = new Promise();
|
Chris@0
|
50 $promise->then(
|
Chris@0
|
51 // $onFulfilled
|
Chris@0
|
52 function ($value) {
|
Chris@0
|
53 echo 'The promise was fulfilled.';
|
Chris@0
|
54 },
|
Chris@0
|
55 // $onRejected
|
Chris@0
|
56 function ($reason) {
|
Chris@0
|
57 echo 'The promise was rejected.';
|
Chris@0
|
58 }
|
Chris@0
|
59 );
|
Chris@0
|
60 ```
|
Chris@0
|
61
|
Chris@0
|
62 *Resolving* a promise means that you either fulfill a promise with a *value* or
|
Chris@0
|
63 reject a promise with a *reason*. Resolving a promises triggers callbacks
|
Chris@0
|
64 registered with the promises's `then` method. These callbacks are triggered
|
Chris@0
|
65 only once and in the order in which they were added.
|
Chris@0
|
66
|
Chris@0
|
67
|
Chris@0
|
68 ## Resolving a promise
|
Chris@0
|
69
|
Chris@0
|
70 Promises are fulfilled using the `resolve($value)` method. Resolving a promise
|
Chris@0
|
71 with any value other than a `GuzzleHttp\Promise\RejectedPromise` will trigger
|
Chris@0
|
72 all of the onFulfilled callbacks (resolving a promise with a rejected promise
|
Chris@0
|
73 will reject the promise and trigger the `$onRejected` callbacks).
|
Chris@0
|
74
|
Chris@0
|
75 ```php
|
Chris@0
|
76 use GuzzleHttp\Promise\Promise;
|
Chris@0
|
77
|
Chris@0
|
78 $promise = new Promise();
|
Chris@0
|
79 $promise
|
Chris@0
|
80 ->then(function ($value) {
|
Chris@0
|
81 // Return a value and don't break the chain
|
Chris@0
|
82 return "Hello, " . $value;
|
Chris@0
|
83 })
|
Chris@0
|
84 // This then is executed after the first then and receives the value
|
Chris@0
|
85 // returned from the first then.
|
Chris@0
|
86 ->then(function ($value) {
|
Chris@0
|
87 echo $value;
|
Chris@0
|
88 });
|
Chris@0
|
89
|
Chris@0
|
90 // Resolving the promise triggers the $onFulfilled callbacks and outputs
|
Chris@0
|
91 // "Hello, reader".
|
Chris@0
|
92 $promise->resolve('reader.');
|
Chris@0
|
93 ```
|
Chris@0
|
94
|
Chris@0
|
95
|
Chris@0
|
96 ## Promise forwarding
|
Chris@0
|
97
|
Chris@0
|
98 Promises can be chained one after the other. Each then in the chain is a new
|
Chris@0
|
99 promise. The return value of a promise is what's forwarded to the next
|
Chris@0
|
100 promise in the chain. Returning a promise in a `then` callback will cause the
|
Chris@0
|
101 subsequent promises in the chain to only be fulfilled when the returned promise
|
Chris@0
|
102 has been fulfilled. The next promise in the chain will be invoked with the
|
Chris@0
|
103 resolved value of the promise.
|
Chris@0
|
104
|
Chris@0
|
105 ```php
|
Chris@0
|
106 use GuzzleHttp\Promise\Promise;
|
Chris@0
|
107
|
Chris@0
|
108 $promise = new Promise();
|
Chris@0
|
109 $nextPromise = new Promise();
|
Chris@0
|
110
|
Chris@0
|
111 $promise
|
Chris@0
|
112 ->then(function ($value) use ($nextPromise) {
|
Chris@0
|
113 echo $value;
|
Chris@0
|
114 return $nextPromise;
|
Chris@0
|
115 })
|
Chris@0
|
116 ->then(function ($value) {
|
Chris@0
|
117 echo $value;
|
Chris@0
|
118 });
|
Chris@0
|
119
|
Chris@0
|
120 // Triggers the first callback and outputs "A"
|
Chris@0
|
121 $promise->resolve('A');
|
Chris@0
|
122 // Triggers the second callback and outputs "B"
|
Chris@0
|
123 $nextPromise->resolve('B');
|
Chris@0
|
124 ```
|
Chris@0
|
125
|
Chris@0
|
126 ## Promise rejection
|
Chris@0
|
127
|
Chris@0
|
128 When a promise is rejected, the `$onRejected` callbacks are invoked with the
|
Chris@0
|
129 rejection reason.
|
Chris@0
|
130
|
Chris@0
|
131 ```php
|
Chris@0
|
132 use GuzzleHttp\Promise\Promise;
|
Chris@0
|
133
|
Chris@0
|
134 $promise = new Promise();
|
Chris@0
|
135 $promise->then(null, function ($reason) {
|
Chris@0
|
136 echo $reason;
|
Chris@0
|
137 });
|
Chris@0
|
138
|
Chris@0
|
139 $promise->reject('Error!');
|
Chris@0
|
140 // Outputs "Error!"
|
Chris@0
|
141 ```
|
Chris@0
|
142
|
Chris@0
|
143 ## Rejection forwarding
|
Chris@0
|
144
|
Chris@0
|
145 If an exception is thrown in an `$onRejected` callback, subsequent
|
Chris@0
|
146 `$onRejected` callbacks are invoked with the thrown exception as the reason.
|
Chris@0
|
147
|
Chris@0
|
148 ```php
|
Chris@0
|
149 use GuzzleHttp\Promise\Promise;
|
Chris@0
|
150
|
Chris@0
|
151 $promise = new Promise();
|
Chris@0
|
152 $promise->then(null, function ($reason) {
|
Chris@0
|
153 throw new \Exception($reason);
|
Chris@0
|
154 })->then(null, function ($reason) {
|
Chris@0
|
155 assert($reason->getMessage() === 'Error!');
|
Chris@0
|
156 });
|
Chris@0
|
157
|
Chris@0
|
158 $promise->reject('Error!');
|
Chris@0
|
159 ```
|
Chris@0
|
160
|
Chris@0
|
161 You can also forward a rejection down the promise chain by returning a
|
Chris@0
|
162 `GuzzleHttp\Promise\RejectedPromise` in either an `$onFulfilled` or
|
Chris@0
|
163 `$onRejected` callback.
|
Chris@0
|
164
|
Chris@0
|
165 ```php
|
Chris@0
|
166 use GuzzleHttp\Promise\Promise;
|
Chris@0
|
167 use GuzzleHttp\Promise\RejectedPromise;
|
Chris@0
|
168
|
Chris@0
|
169 $promise = new Promise();
|
Chris@0
|
170 $promise->then(null, function ($reason) {
|
Chris@0
|
171 return new RejectedPromise($reason);
|
Chris@0
|
172 })->then(null, function ($reason) {
|
Chris@0
|
173 assert($reason === 'Error!');
|
Chris@0
|
174 });
|
Chris@0
|
175
|
Chris@0
|
176 $promise->reject('Error!');
|
Chris@0
|
177 ```
|
Chris@0
|
178
|
Chris@0
|
179 If an exception is not thrown in a `$onRejected` callback and the callback
|
Chris@0
|
180 does not return a rejected promise, downstream `$onFulfilled` callbacks are
|
Chris@0
|
181 invoked using the value returned from the `$onRejected` callback.
|
Chris@0
|
182
|
Chris@0
|
183 ```php
|
Chris@0
|
184 use GuzzleHttp\Promise\Promise;
|
Chris@0
|
185 use GuzzleHttp\Promise\RejectedPromise;
|
Chris@0
|
186
|
Chris@0
|
187 $promise = new Promise();
|
Chris@0
|
188 $promise
|
Chris@0
|
189 ->then(null, function ($reason) {
|
Chris@0
|
190 return "It's ok";
|
Chris@0
|
191 })
|
Chris@0
|
192 ->then(function ($value) {
|
Chris@0
|
193 assert($value === "It's ok");
|
Chris@0
|
194 });
|
Chris@0
|
195
|
Chris@0
|
196 $promise->reject('Error!');
|
Chris@0
|
197 ```
|
Chris@0
|
198
|
Chris@0
|
199 # Synchronous wait
|
Chris@0
|
200
|
Chris@0
|
201 You can synchronously force promises to complete using a promise's `wait`
|
Chris@0
|
202 method. When creating a promise, you can provide a wait function that is used
|
Chris@0
|
203 to synchronously force a promise to complete. When a wait function is invoked
|
Chris@0
|
204 it is expected to deliver a value to the promise or reject the promise. If the
|
Chris@0
|
205 wait function does not deliver a value, then an exception is thrown. The wait
|
Chris@0
|
206 function provided to a promise constructor is invoked when the `wait` function
|
Chris@0
|
207 of the promise is called.
|
Chris@0
|
208
|
Chris@0
|
209 ```php
|
Chris@0
|
210 $promise = new Promise(function () use (&$promise) {
|
Chris@0
|
211 $promise->resolve('foo');
|
Chris@0
|
212 });
|
Chris@0
|
213
|
Chris@0
|
214 // Calling wait will return the value of the promise.
|
Chris@0
|
215 echo $promise->wait(); // outputs "foo"
|
Chris@0
|
216 ```
|
Chris@0
|
217
|
Chris@0
|
218 If an exception is encountered while invoking the wait function of a promise,
|
Chris@0
|
219 the promise is rejected with the exception and the exception is thrown.
|
Chris@0
|
220
|
Chris@0
|
221 ```php
|
Chris@0
|
222 $promise = new Promise(function () use (&$promise) {
|
Chris@0
|
223 throw new \Exception('foo');
|
Chris@0
|
224 });
|
Chris@0
|
225
|
Chris@0
|
226 $promise->wait(); // throws the exception.
|
Chris@0
|
227 ```
|
Chris@0
|
228
|
Chris@0
|
229 Calling `wait` on a promise that has been fulfilled will not trigger the wait
|
Chris@0
|
230 function. It will simply return the previously resolved value.
|
Chris@0
|
231
|
Chris@0
|
232 ```php
|
Chris@0
|
233 $promise = new Promise(function () { die('this is not called!'); });
|
Chris@0
|
234 $promise->resolve('foo');
|
Chris@0
|
235 echo $promise->wait(); // outputs "foo"
|
Chris@0
|
236 ```
|
Chris@0
|
237
|
Chris@0
|
238 Calling `wait` on a promise that has been rejected will throw an exception. If
|
Chris@0
|
239 the rejection reason is an instance of `\Exception` the reason is thrown.
|
Chris@0
|
240 Otherwise, a `GuzzleHttp\Promise\RejectionException` is thrown and the reason
|
Chris@0
|
241 can be obtained by calling the `getReason` method of the exception.
|
Chris@0
|
242
|
Chris@0
|
243 ```php
|
Chris@0
|
244 $promise = new Promise();
|
Chris@0
|
245 $promise->reject('foo');
|
Chris@0
|
246 $promise->wait();
|
Chris@0
|
247 ```
|
Chris@0
|
248
|
Chris@0
|
249 > PHP Fatal error: Uncaught exception 'GuzzleHttp\Promise\RejectionException' with message 'The promise was rejected with value: foo'
|
Chris@0
|
250
|
Chris@0
|
251
|
Chris@0
|
252 ## Unwrapping a promise
|
Chris@0
|
253
|
Chris@0
|
254 When synchronously waiting on a promise, you are joining the state of the
|
Chris@0
|
255 promise into the current state of execution (i.e., return the value of the
|
Chris@0
|
256 promise if it was fulfilled or throw an exception if it was rejected). This is
|
Chris@0
|
257 called "unwrapping" the promise. Waiting on a promise will by default unwrap
|
Chris@0
|
258 the promise state.
|
Chris@0
|
259
|
Chris@0
|
260 You can force a promise to resolve and *not* unwrap the state of the promise
|
Chris@0
|
261 by passing `false` to the first argument of the `wait` function:
|
Chris@0
|
262
|
Chris@0
|
263 ```php
|
Chris@0
|
264 $promise = new Promise();
|
Chris@0
|
265 $promise->reject('foo');
|
Chris@0
|
266 // This will not throw an exception. It simply ensures the promise has
|
Chris@0
|
267 // been resolved.
|
Chris@0
|
268 $promise->wait(false);
|
Chris@0
|
269 ```
|
Chris@0
|
270
|
Chris@0
|
271 When unwrapping a promise, the resolved value of the promise will be waited
|
Chris@0
|
272 upon until the unwrapped value is not a promise. This means that if you resolve
|
Chris@0
|
273 promise A with a promise B and unwrap promise A, the value returned by the
|
Chris@0
|
274 wait function will be the value delivered to promise B.
|
Chris@0
|
275
|
Chris@0
|
276 **Note**: when you do not unwrap the promise, no value is returned.
|
Chris@0
|
277
|
Chris@0
|
278
|
Chris@0
|
279 # Cancellation
|
Chris@0
|
280
|
Chris@0
|
281 You can cancel a promise that has not yet been fulfilled using the `cancel()`
|
Chris@0
|
282 method of a promise. When creating a promise you can provide an optional
|
Chris@0
|
283 cancel function that when invoked cancels the action of computing a resolution
|
Chris@0
|
284 of the promise.
|
Chris@0
|
285
|
Chris@0
|
286
|
Chris@0
|
287 # API
|
Chris@0
|
288
|
Chris@0
|
289
|
Chris@0
|
290 ## Promise
|
Chris@0
|
291
|
Chris@0
|
292 When creating a promise object, you can provide an optional `$waitFn` and
|
Chris@0
|
293 `$cancelFn`. `$waitFn` is a function that is invoked with no arguments and is
|
Chris@0
|
294 expected to resolve the promise. `$cancelFn` is a function with no arguments
|
Chris@0
|
295 that is expected to cancel the computation of a promise. It is invoked when the
|
Chris@0
|
296 `cancel()` method of a promise is called.
|
Chris@0
|
297
|
Chris@0
|
298 ```php
|
Chris@0
|
299 use GuzzleHttp\Promise\Promise;
|
Chris@0
|
300
|
Chris@0
|
301 $promise = new Promise(
|
Chris@0
|
302 function () use (&$promise) {
|
Chris@0
|
303 $promise->resolve('waited');
|
Chris@0
|
304 },
|
Chris@0
|
305 function () {
|
Chris@0
|
306 // do something that will cancel the promise computation (e.g., close
|
Chris@0
|
307 // a socket, cancel a database query, etc...)
|
Chris@0
|
308 }
|
Chris@0
|
309 );
|
Chris@0
|
310
|
Chris@0
|
311 assert('waited' === $promise->wait());
|
Chris@0
|
312 ```
|
Chris@0
|
313
|
Chris@0
|
314 A promise has the following methods:
|
Chris@0
|
315
|
Chris@0
|
316 - `then(callable $onFulfilled, callable $onRejected) : PromiseInterface`
|
Chris@0
|
317
|
Chris@0
|
318 Appends fulfillment and rejection handlers to the promise, and returns a new promise resolving to the return value of the called handler.
|
Chris@0
|
319
|
Chris@0
|
320 - `otherwise(callable $onRejected) : PromiseInterface`
|
Chris@0
|
321
|
Chris@0
|
322 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.
|
Chris@0
|
323
|
Chris@0
|
324 - `wait($unwrap = true) : mixed`
|
Chris@0
|
325
|
Chris@0
|
326 Synchronously waits on the promise to complete.
|
Chris@0
|
327
|
Chris@0
|
328 `$unwrap` controls whether or not the value of the promise is returned for a
|
Chris@0
|
329 fulfilled promise or if an exception is thrown if the promise is rejected.
|
Chris@0
|
330 This is set to `true` by default.
|
Chris@0
|
331
|
Chris@0
|
332 - `cancel()`
|
Chris@0
|
333
|
Chris@0
|
334 Attempts to cancel the promise if possible. The promise being cancelled and
|
Chris@0
|
335 the parent most ancestor that has not yet been resolved will also be
|
Chris@0
|
336 cancelled. Any promises waiting on the cancelled promise to resolve will also
|
Chris@0
|
337 be cancelled.
|
Chris@0
|
338
|
Chris@0
|
339 - `getState() : string`
|
Chris@0
|
340
|
Chris@0
|
341 Returns the state of the promise. One of `pending`, `fulfilled`, or
|
Chris@0
|
342 `rejected`.
|
Chris@0
|
343
|
Chris@0
|
344 - `resolve($value)`
|
Chris@0
|
345
|
Chris@0
|
346 Fulfills the promise with the given `$value`.
|
Chris@0
|
347
|
Chris@0
|
348 - `reject($reason)`
|
Chris@0
|
349
|
Chris@0
|
350 Rejects the promise with the given `$reason`.
|
Chris@0
|
351
|
Chris@0
|
352
|
Chris@0
|
353 ## FulfilledPromise
|
Chris@0
|
354
|
Chris@0
|
355 A fulfilled promise can be created to represent a promise that has been
|
Chris@0
|
356 fulfilled.
|
Chris@0
|
357
|
Chris@0
|
358 ```php
|
Chris@0
|
359 use GuzzleHttp\Promise\FulfilledPromise;
|
Chris@0
|
360
|
Chris@0
|
361 $promise = new FulfilledPromise('value');
|
Chris@0
|
362
|
Chris@0
|
363 // Fulfilled callbacks are immediately invoked.
|
Chris@0
|
364 $promise->then(function ($value) {
|
Chris@0
|
365 echo $value;
|
Chris@0
|
366 });
|
Chris@0
|
367 ```
|
Chris@0
|
368
|
Chris@0
|
369
|
Chris@0
|
370 ## RejectedPromise
|
Chris@0
|
371
|
Chris@0
|
372 A rejected promise can be created to represent a promise that has been
|
Chris@0
|
373 rejected.
|
Chris@0
|
374
|
Chris@0
|
375 ```php
|
Chris@0
|
376 use GuzzleHttp\Promise\RejectedPromise;
|
Chris@0
|
377
|
Chris@0
|
378 $promise = new RejectedPromise('Error');
|
Chris@0
|
379
|
Chris@0
|
380 // Rejected callbacks are immediately invoked.
|
Chris@0
|
381 $promise->then(null, function ($reason) {
|
Chris@0
|
382 echo $reason;
|
Chris@0
|
383 });
|
Chris@0
|
384 ```
|
Chris@0
|
385
|
Chris@0
|
386
|
Chris@0
|
387 # Promise interop
|
Chris@0
|
388
|
Chris@0
|
389 This library works with foreign promises that have a `then` method. This means
|
Chris@0
|
390 you can use Guzzle promises with [React promises](https://github.com/reactphp/promise)
|
Chris@0
|
391 for example. When a foreign promise is returned inside of a then method
|
Chris@0
|
392 callback, promise resolution will occur recursively.
|
Chris@0
|
393
|
Chris@0
|
394 ```php
|
Chris@0
|
395 // Create a React promise
|
Chris@0
|
396 $deferred = new React\Promise\Deferred();
|
Chris@0
|
397 $reactPromise = $deferred->promise();
|
Chris@0
|
398
|
Chris@0
|
399 // Create a Guzzle promise that is fulfilled with a React promise.
|
Chris@0
|
400 $guzzlePromise = new \GuzzleHttp\Promise\Promise();
|
Chris@0
|
401 $guzzlePromise->then(function ($value) use ($reactPromise) {
|
Chris@0
|
402 // Do something something with the value...
|
Chris@0
|
403 // Return the React promise
|
Chris@0
|
404 return $reactPromise;
|
Chris@0
|
405 });
|
Chris@0
|
406 ```
|
Chris@0
|
407
|
Chris@0
|
408 Please note that wait and cancel chaining is no longer possible when forwarding
|
Chris@0
|
409 a foreign promise. You will need to wrap a third-party promise with a Guzzle
|
Chris@0
|
410 promise in order to utilize wait and cancel functions with foreign promises.
|
Chris@0
|
411
|
Chris@0
|
412
|
Chris@0
|
413 ## Event Loop Integration
|
Chris@0
|
414
|
Chris@0
|
415 In order to keep the stack size constant, Guzzle promises are resolved
|
Chris@0
|
416 asynchronously using a task queue. When waiting on promises synchronously, the
|
Chris@0
|
417 task queue will be automatically run to ensure that the blocking promise and
|
Chris@0
|
418 any forwarded promises are resolved. When using promises asynchronously in an
|
Chris@0
|
419 event loop, you will need to run the task queue on each tick of the loop. If
|
Chris@0
|
420 you do not run the task queue, then promises will not be resolved.
|
Chris@0
|
421
|
Chris@0
|
422 You can run the task queue using the `run()` method of the global task queue
|
Chris@0
|
423 instance.
|
Chris@0
|
424
|
Chris@0
|
425 ```php
|
Chris@0
|
426 // Get the global task queue
|
Chris@0
|
427 $queue = \GuzzleHttp\Promise\queue();
|
Chris@0
|
428 $queue->run();
|
Chris@0
|
429 ```
|
Chris@0
|
430
|
Chris@0
|
431 For example, you could use Guzzle promises with React using a periodic timer:
|
Chris@0
|
432
|
Chris@0
|
433 ```php
|
Chris@0
|
434 $loop = React\EventLoop\Factory::create();
|
Chris@0
|
435 $loop->addPeriodicTimer(0, [$queue, 'run']);
|
Chris@0
|
436 ```
|
Chris@0
|
437
|
Chris@0
|
438 *TODO*: Perhaps adding a `futureTick()` on each tick would be faster?
|
Chris@0
|
439
|
Chris@0
|
440
|
Chris@0
|
441 # Implementation notes
|
Chris@0
|
442
|
Chris@0
|
443
|
Chris@0
|
444 ## Promise resolution and chaining is handled iteratively
|
Chris@0
|
445
|
Chris@0
|
446 By shuffling pending handlers from one owner to another, promises are
|
Chris@0
|
447 resolved iteratively, allowing for "infinite" then chaining.
|
Chris@0
|
448
|
Chris@0
|
449 ```php
|
Chris@0
|
450 <?php
|
Chris@0
|
451 require 'vendor/autoload.php';
|
Chris@0
|
452
|
Chris@0
|
453 use GuzzleHttp\Promise\Promise;
|
Chris@0
|
454
|
Chris@0
|
455 $parent = new Promise();
|
Chris@0
|
456 $p = $parent;
|
Chris@0
|
457
|
Chris@0
|
458 for ($i = 0; $i < 1000; $i++) {
|
Chris@0
|
459 $p = $p->then(function ($v) {
|
Chris@0
|
460 // The stack size remains constant (a good thing)
|
Chris@0
|
461 echo xdebug_get_stack_depth() . ', ';
|
Chris@0
|
462 return $v + 1;
|
Chris@0
|
463 });
|
Chris@0
|
464 }
|
Chris@0
|
465
|
Chris@0
|
466 $parent->resolve(0);
|
Chris@0
|
467 var_dump($p->wait()); // int(1000)
|
Chris@0
|
468
|
Chris@0
|
469 ```
|
Chris@0
|
470
|
Chris@0
|
471 When a promise is fulfilled or rejected with a non-promise value, the promise
|
Chris@0
|
472 then takes ownership of the handlers of each child promise and delivers values
|
Chris@0
|
473 down the chain without using recursion.
|
Chris@0
|
474
|
Chris@0
|
475 When a promise is resolved with another promise, the original promise transfers
|
Chris@0
|
476 all of its pending handlers to the new promise. When the new promise is
|
Chris@0
|
477 eventually resolved, all of the pending handlers are delivered the forwarded
|
Chris@0
|
478 value.
|
Chris@0
|
479
|
Chris@0
|
480
|
Chris@0
|
481 ## A promise is the deferred.
|
Chris@0
|
482
|
Chris@0
|
483 Some promise libraries implement promises using a deferred object to represent
|
Chris@0
|
484 a computation and a promise object to represent the delivery of the result of
|
Chris@0
|
485 the computation. This is a nice separation of computation and delivery because
|
Chris@0
|
486 consumers of the promise cannot modify the value that will be eventually
|
Chris@0
|
487 delivered.
|
Chris@0
|
488
|
Chris@0
|
489 One side effect of being able to implement promise resolution and chaining
|
Chris@0
|
490 iteratively is that you need to be able for one promise to reach into the state
|
Chris@0
|
491 of another promise to shuffle around ownership of handlers. In order to achieve
|
Chris@0
|
492 this without making the handlers of a promise publicly mutable, a promise is
|
Chris@0
|
493 also the deferred value, allowing promises of the same parent class to reach
|
Chris@0
|
494 into and modify the private properties of promises of the same type. While this
|
Chris@0
|
495 does allow consumers of the value to modify the resolution or rejection of the
|
Chris@0
|
496 deferred, it is a small price to pay for keeping the stack size constant.
|
Chris@0
|
497
|
Chris@0
|
498 ```php
|
Chris@0
|
499 $promise = new Promise();
|
Chris@0
|
500 $promise->then(function ($value) { echo $value; });
|
Chris@0
|
501 // The promise is the deferred value, so you can deliver a value to it.
|
Chris@0
|
502 $promise->resolve('foo');
|
Chris@0
|
503 // prints "foo"
|
Chris@0
|
504 ```
|