Skip to content

Commit 54edf77

Browse files
committed
Extend readme with functions and promise sections
1 parent ffa9a90 commit 54edf77

1 file changed

Lines changed: 181 additions & 0 deletions

File tree

README.md

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ Table of Contents
1818
* [Deferred](#deferred-1)
1919
* [Promise](#promise-1)
2020
* [Resolver](#resolver-1)
21+
* [Functions](#functions)
22+
* [resolve()](#resolve)
23+
* [reject()](#reject)
24+
* [all()](#all)
25+
* [race()](#race)
26+
* [any()](#any)
27+
* [some()](#some)
28+
* [map()](#map)
29+
* [reduce()](#reduce)
2130
* [When](#when)
2231
* [When::all()](#whenall)
2332
* [When::any()](#whenany)
@@ -109,6 +118,38 @@ The Promise represents the eventual outcome, which is either fulfillment
109118
reason. The Promise provides mechanisms for arranging to call a function on its
110119
value or reason, and produces a new Promise for the result.
111120

121+
Creates a promise whose state is controlled by the functions passed to
122+
`$resolver`.
123+
124+
```php
125+
$resolver = function (callable $resolve, callable $reject, callable $notify) {
126+
// Do some work, possibly asynchronously, and then
127+
// resolve or reject. You can notify of progress events
128+
// along the way if you want/need.
129+
130+
$resolve($awesomeResult);
131+
// or $resolve($anotherPromise);
132+
// or $reject($nastyError);
133+
// or $notify($progressNotification);
134+
};
135+
136+
$promise = new React\Promise\Promise($resolver);
137+
```
138+
139+
The promise constructor receives a resolver function which will be called
140+
immediately with 3 arguments:
141+
142+
* `$resolve($value)` - Primary function that seals the fate of the
143+
returned promise. Accepts either a non-promise value, or another promise.
144+
When called with a non-promise value, fulfills promise with that value.
145+
When called with another promise, e.g. `$resolve($otherPromise)`, promise's
146+
fate will be equivalent to that of `$otherPromise`.
147+
* `$reject($reason)` - Function that rejects the promise.
148+
* `$notify($update)` - Function that issues progress events for the promise.
149+
150+
If the resolver throws an exception, the promise will be rejected with that
151+
thrown exception as the rejection reason.
152+
112153
A Promise has a single method `then()` which registers new fulfilled, error and
113154
progress handlers with this Promise (all parameters are optional):
114155

@@ -184,17 +225,136 @@ is making progress toward its result.
184225
All consumers are notified by having their `$progressHandler` (which they
185226
registered via `$promise->then()`) called with `$update`.
186227

228+
### Functions
229+
230+
Useful functions for creating, joining, mapping and reducing collections of
231+
promises.
232+
233+
#### resolve()
234+
235+
```php
236+
$promise = React\Promise\resolve(mixed $promiseOrValue);
237+
```
238+
239+
Creates a promise for the supplied `$promiseOrValue`.
240+
241+
If `$promiseOrValue` is a value, it will be the resolution value of the
242+
returned promise.
243+
244+
If `$promiseOrValue` is a promise, it will simply be returned.
245+
246+
Note: The promise returned is always a promise implementing
247+
[ExtendedPromiseInterface](#extendedpromiseinterface). If you pass in a custom
248+
promise which only implements [PromiseInterface](#promiseinterface), this
249+
promise will be assimilated to a extended promise following `$promiseOrValue`.
250+
251+
#### reject()
252+
253+
```php
254+
$promise = React\Promise\reject(mixed $promiseOrValue);
255+
```
256+
257+
Creates a rejected promise for the supplied `$promiseOrValue`.
258+
259+
If `$promiseOrValue` is a value, it will be the rejection value of the
260+
returned promise.
261+
262+
If `$promiseOrValue` is a promise, its completion value will be the rejected
263+
value of the returned promise.
264+
265+
This can be useful in situations where you need to reject a promise without
266+
throwing an exception. For example, it allows you to propagate a rejection with
267+
the value of another promise.
268+
269+
#### all()
270+
271+
```php
272+
$promise = React\Promise\all(array|React\Promise\PromiseInterface $promisesOrValues);
273+
```
274+
275+
Returns a promise that will resolve only once all the items in
276+
`$promisesOrValues` have resolved. The resolution value of the returned promise
277+
will be an array containing the resolution values of each of the items in
278+
`$promisesOrValues`.
279+
280+
#### race()
281+
282+
```php
283+
$promise = React\Promise\race(array|React\Promise\PromiseInterface $promisesOrValues);
284+
```
285+
286+
Initiates a competitive race that allows one winner. Returns a promise which is
287+
resolved in the same way the first settled promise resolves.
288+
289+
#### any()
290+
291+
```php
292+
$promise = React\Promise\any(array|React\Promise\PromiseInterface $promisesOrValues);
293+
```
294+
295+
Returns a promise that will resolve when any one of the items in
296+
`$promisesOrValues` resolves. The resolution value of the returned promise
297+
will be the resolution value of the triggering item.
298+
299+
The returned promise will only reject if *all* items in `$promisesOrValues` are
300+
rejected. The rejection value will be an array of all rejection reasons.
301+
302+
#### some()
303+
304+
```php
305+
$promise = React\Promise\some(array|React\Promise\PromiseInterface $promisesOrValues, integer $howMany);
306+
```
307+
308+
Returns a promise that will resolve when `$howMany` of the supplied items in
309+
`$promisesOrValues` resolve. The resolution value of the returned promise
310+
will be an array of length `$howMany` containing the resolution values of the
311+
triggering items.
312+
313+
The returned promise will reject if it becomes impossible for `$howMany` items
314+
to resolve (that is, when `(count($promisesOrValues) - $howMany) + 1` items
315+
reject). The rejection value will be an array of
316+
`(count($promisesOrValues) - $howMany) + 1` rejection reasons.
317+
318+
#### map()
319+
320+
```php
321+
$promise = React\Promise\map(array|React\Promise\PromiseInterface $promisesOrValues, callable $mapFunc);
322+
```
323+
324+
Traditional map function, similar to `array_map()`, but allows input to contain
325+
promises and/or values, and `$mapFunc` may return either a value or a promise.
326+
327+
The map function receives each item as argument, where item is a fully resolved
328+
value of a promise or value in `$promisesOrValues`.
329+
330+
#### reduce()
331+
332+
```php
333+
$promise = React\Promise\reduce(array|React\Promise\PromiseInterface $promisesOrValues, callable $reduceFunc , $initialValue = null);
334+
```
335+
336+
Traditional reduce function, similar to `array_reduce()`, but input may contain
337+
promises and/or values, and `$reduceFunc` may return either a value or a
338+
promise, *and* `$initialValue` may be a promise or a value for the starting
339+
value.
340+
187341
### When
188342

189343
The `React\Promise\When` class provides useful methods for creating, joining,
190344
mapping and reducing collections of Promises.
191345

346+
**Note:** Since version 1.1 `React\Promise\When` acts only as a proxy for
347+
the [Promise functions](#functions) and its usage is discouraged.
348+
192349
#### When::all()
193350

194351
``` php
195352
$promise = React\Promise\When::all(array|React\Promise\PromiseInterface $promisesOrValues, callable $fulfilledHandler = null, callable $errorHandler = null, callable $progressHandler = null);
196353
```
197354

355+
**Note:** Since version 1.1, [`React\Promise\all()`](#all) is preferred
356+
over `React\Promise\When::all()`.
357+
198358
Returns a Promise that will resolve only once all the items in
199359
`$promisesOrValues` have resolved. The resolution value of the returned Promise
200360
will be an array containing the resolution values of each of the items in
@@ -206,6 +366,9 @@ will be an array containing the resolution values of each of the items in
206366
$promise = React\Promise\When::any(array|React\Promise\PromiseInterface $promisesOrValues, callable $fulfilledHandler = null, callable $errorHandler = null, callable $progressHandler = null);
207367
```
208368

369+
**Note:** Since version 1.1, [`React\Promise\any()`](#any) is preferred
370+
over `React\Promise\When::any()`.
371+
209372
Returns a Promise that will resolve when any one of the items in
210373
`$promisesOrValues` resolves. The resolution value of the returned Promise
211374
will be the resolution value of the triggering item.
@@ -219,6 +382,9 @@ rejected. The rejection value will be an array of all rejection reasons.
219382
$promise = React\Promise\When::some(array|React\Promise\PromiseInterface $promisesOrValues, integer $howMany, callable $fulfilledHandler = null, callable $errorHandler = null, callable $progressHandler = null);
220383
```
221384

385+
**Note:** Since version 1.1, [`React\Promise\some()`](#some) is preferred
386+
over `React\Promise\When::some()`.
387+
222388
Returns a Promise that will resolve when `$howMany` of the supplied items in
223389
`$promisesOrValues` resolve. The resolution value of the returned Promise
224390
will be an array of length `$howMany` containing the resolution values of the
@@ -235,6 +401,9 @@ reject). The rejection value will be an array of
235401
$promise = React\Promise\When::map(array|React\Promise\PromiseInterface $promisesOrValues, callable $mapFunc);
236402
```
237403

404+
**Note:** Since version 1.1, [`React\Promise\map()`](#map) is preferred
405+
over `React\Promise\When::map()`.
406+
238407
Traditional map function, similar to `array_map()`, but allows input to contain
239408
Promises and/or values, and `$mapFunc` may return either a value or a Promise.
240409

@@ -247,6 +416,9 @@ value of a Promise or value in `$promisesOrValues`.
247416
$promise = React\Promise\When::reduce(array|React\Promise\PromiseInterface $promisesOrValues, callable $reduceFunc , $initialValue = null);
248417
```
249418

419+
**Note:** Since version 1.1, [`React\Promise\reduce()`](#reduce) is preferred
420+
over `React\Promise\When::reduce()`.
421+
250422
Traditional reduce function, similar to `array_reduce()`, but input may contain
251423
Promises and/or values, and `$reduceFunc` may return either a value or a
252424
Promise, *and* `$initialValue` may be a Promise or a value for the starting
@@ -258,6 +430,9 @@ value.
258430
$promise = React\Promise\When::resolve(mixed $promiseOrValue);
259431
```
260432

433+
**Note:** Since version 1.1, [`React\Promise\resolve()`](#resolve) is preferred
434+
over `React\Promise\When::resolve()`.
435+
261436
Creates a resolved Promise for the supplied `$promiseOrValue`.
262437

263438
If `$promiseOrValue` is a value, it will be the resolution value of the
@@ -271,6 +446,9 @@ If `$promiseOrValue` is a Promise, it will simply be returned.
271446
$promise = React\Promise\When::reject(mixed $promiseOrValue);
272447
```
273448

449+
**Note:** Since version 1.1, [`React\Promise\reject()`](#reject) is preferred
450+
over `React\Promise\When::reject()`.
451+
274452
Creates a rejected Promise for the supplied `$promiseOrValue`.
275453

276454
If `$promiseOrValue` is a value, it will be the rejection value of the
@@ -289,6 +467,9 @@ the value of another Promise.
289467
$promise = React\Promise\When::lazy(callable $factory);
290468
```
291469

470+
**Note:** Since version 1.1, [`React\Promise\lazy()`](#lazy) is preferred
471+
over `React\Promise\When::lazy()`.
472+
292473
Creates a Promise which will be lazily initialized by `$factory` once a consumer
293474
calls the `then()` method.
294475

0 commit comments

Comments
 (0)