22
33namespace React \Promise ;
44
5+ /**
6+ * Creates a promise for the supplied `$promiseOrValue`.
7+ *
8+ * If `$promiseOrValue` is a value, it will be the resolution value of the
9+ * returned promise.
10+ *
11+ * If `$promiseOrValue` is a thenable (any object that provides a `then()` method),
12+ * a trusted promise that follows the state of the thenable is returned.
13+ *
14+ * If `$promiseOrValue` is a promise, it will be returned as is.
15+ *
16+ * @param mixed $promiseOrValue
17+ * @return PromiseInterface
18+ */
519function resolve ($ promiseOrValue = null )
620{
721 if ($ promiseOrValue instanceof PromiseInterface) {
@@ -23,6 +37,22 @@ function resolve($promiseOrValue = null)
2337 return new FulfilledPromise ($ promiseOrValue );
2438}
2539
40+ /**
41+ * Creates a rejected promise for the supplied `$promiseOrValue`.
42+ *
43+ * If `$promiseOrValue` is a value, it will be the rejection value of the
44+ * returned promise.
45+ *
46+ * If `$promiseOrValue` is a promise, its completion value will be the rejected
47+ * value of the returned promise.
48+ *
49+ * This can be useful in situations where you need to reject a promise without
50+ * throwing an exception. For example, it allows you to propagate a rejection with
51+ * the value of another promise.
52+ *
53+ * @param mixed $promiseOrValue
54+ * @return PromiseInterface
55+ */
2656function reject ($ promiseOrValue = null )
2757{
2858 if ($ promiseOrValue instanceof PromiseInterface) {
@@ -34,13 +64,32 @@ function reject($promiseOrValue = null)
3464 return new RejectedPromise ($ promiseOrValue );
3565}
3666
67+ /**
68+ * Returns a promise that will resolve only once all the items in
69+ * `$promisesOrValues` have resolved. The resolution value of the returned promise
70+ * will be an array containing the resolution values of each of the items in
71+ * `$promisesOrValues`.
72+ *
73+ * @param array $promisesOrValues
74+ * @return PromiseInterface
75+ */
3776function all (array $ promisesOrValues )
3877{
3978 return map ($ promisesOrValues , function ($ val ) {
4079 return $ val ;
4180 });
4281}
4382
83+ /**
84+ * Initiates a competitive race that allows one winner. Returns a promise which is
85+ * resolved in the same way the first settled promise resolves.
86+ *
87+ * The returned promise will become **infinitely pending** if `$promisesOrValues`
88+ * contains 0 items.
89+ *
90+ * @param array $promisesOrValues
91+ * @return PromiseInterface
92+ */
4493function race (array $ promisesOrValues )
4594{
4695 if (!$ promisesOrValues ) {
@@ -59,6 +108,20 @@ function race(array $promisesOrValues)
59108 }, $ cancellationQueue );
60109}
61110
111+ /**
112+ * Returns a promise that will resolve when any one of the items in
113+ * `$promisesOrValues` resolves. The resolution value of the returned promise
114+ * will be the resolution value of the triggering item.
115+ *
116+ * The returned promise will only reject if *all* items in `$promisesOrValues` are
117+ * rejected. The rejection value will be an array of all rejection reasons.
118+ *
119+ * The returned promise will also reject with a `React\Promise\Exception\LengthException`
120+ * if `$promisesOrValues` contains 0 items.
121+ *
122+ * @param array $promisesOrValues
123+ * @return PromiseInterface
124+ */
62125function any (array $ promisesOrValues )
63126{
64127 return some ($ promisesOrValues , 1 )
@@ -67,6 +130,24 @@ function any(array $promisesOrValues)
67130 });
68131}
69132
133+ /**
134+ * Returns a promise that will resolve when `$howMany` of the supplied items in
135+ * `$promisesOrValues` resolve. The resolution value of the returned promise
136+ * will be an array of length `$howMany` containing the resolution values of the
137+ * triggering items.
138+ *
139+ * The returned promise will reject if it becomes impossible for `$howMany` items
140+ * to resolve (that is, when `(count($promisesOrValues) - $howMany) + 1` items
141+ * reject). The rejection value will be an array of
142+ * `(count($promisesOrValues) - $howMany) + 1` rejection reasons.
143+ *
144+ * The returned promise will also reject with a `React\Promise\Exception\LengthException`
145+ * if `$promisesOrValues` contains less items than `$howMany`.
146+ *
147+ * @param array $promisesOrValues
148+ * @param int $howMany
149+ * @return PromiseInterface
150+ */
70151function some (array $ promisesOrValues , $ howMany )
71152{
72153 if ($ howMany < 1 ) {
@@ -130,6 +211,17 @@ function some(array $promisesOrValues, $howMany)
130211 }, $ cancellationQueue );
131212}
132213
214+ /**
215+ * Traditional map function, similar to `array_map()`, but allows input to contain
216+ * promises and/or values, and `$mapFunc` may return either a value or a promise.
217+ *
218+ * The map function receives each item as argument, where item is a fully resolved
219+ * value of a promise or value in `$promisesOrValues`.
220+ *
221+ * @param array $promisesOrValues
222+ * @param callable $mapFunc
223+ * @return PromiseInterface
224+ */
133225function map (array $ promisesOrValues , callable $ mapFunc )
134226{
135227 if (!$ promisesOrValues ) {
@@ -162,6 +254,17 @@ function ($mapped) use ($i, &$values, &$toResolve, $resolve) {
162254 }, $ cancellationQueue );
163255}
164256
257+ /**
258+ * Traditional reduce function, similar to `array_reduce()`, but input may contain
259+ * promises and/or values, and `$reduceFunc` may return either a value or a
260+ * promise, *and* `$initialValue` may be a promise or a value for the starting
261+ * value.
262+ *
263+ * @param array $promisesOrValues
264+ * @param callable $reduceFunc
265+ * @param mixed $initialValue
266+ * @return PromiseInterface
267+ */
165268function reduce (array $ promisesOrValues , callable $ reduceFunc , $ initialValue = null )
166269{
167270 $ cancellationQueue = new Internal \CancellationQueue ();
0 commit comments