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