Skip to content

Commit c2608dd

Browse files
authored
Merge pull request #132 from CharlotteDunoisLabs/patch-docblocks
Add docblocks to functions and interfaces
2 parents 368a5cb + ded594b commit c2608dd

File tree

3 files changed

+199
-0
lines changed

3 files changed

+199
-0
lines changed

src/PromiseInterface.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,120 @@
55
interface PromiseInterface
66
{
77
/**
8+
* Transforms a promise's value by applying a function to the promise's fulfillment
9+
* or rejection value. Returns a new promise for the transformed result.
10+
*
11+
* The `then()` method registers new fulfilled and rejection handlers with a promise
12+
* (all parameters are optional):
13+
*
14+
* * `$onFulfilled` will be invoked once the promise is fulfilled and passed
15+
* the result as the first argument.
16+
* * `$onRejected` will be invoked once the promise is rejected and passed the
17+
* reason as the first argument.
18+
*
19+
* It returns a new promise that will fulfill with the return value of either
20+
* `$onFulfilled` or `$onRejected`, whichever is called, or will reject with
21+
* the thrown exception if either throws.
22+
*
23+
* A promise makes the following guarantees about handlers registered in
24+
* the same call to `then()`:
25+
*
26+
* 1. Only one of `$onFulfilled` or `$onRejected` will be called,
27+
* never both.
28+
* 2. `$onFulfilled` and `$onRejected` will never be called more
29+
* than once.
30+
*
31+
* @param callable|null $onFulfilled
32+
* @param callable|null $onRejected
833
* @return PromiseInterface
934
*/
1035
public function then(callable $onFulfilled = null, callable $onRejected = null);
1136

1237
/**
38+
* Consumes the promise's ultimate value if the promise fulfills, or handles the
39+
* ultimate error.
40+
*
41+
* It will cause a fatal error (`E_USER_ERROR`) if either `$onFulfilled` or
42+
* `$onRejected` throw or return a rejected promise.
43+
*
44+
* Since the purpose of `done()` is consumption rather than transformation,
45+
* `done()` always returns `null`.
46+
*
47+
* @param callable|null $onFulfilled
48+
* @param callable|null $onRejected
1349
* @return void
1450
*/
1551
public function done(callable $onFulfilled = null, callable $onRejected = null);
1652

1753
/**
54+
* Registers a rejection handler for promise. It is a shortcut for:
55+
*
56+
* ```php
57+
* $promise->then(null, $onRejected);
58+
* ```
59+
*
60+
* Additionally, you can type hint the `$reason` argument of `$onRejected` to catch
61+
* only specific errors.
62+
*
63+
* @param callable $onRejected
1864
* @return PromiseInterface
1965
*/
2066
public function otherwise(callable $onRejected);
2167

2268
/**
69+
* Allows you to execute "cleanup" type tasks in a promise chain.
70+
*
71+
* It arranges for `$onFulfilledOrRejected` to be called, with no arguments,
72+
* when the promise is either fulfilled or rejected.
73+
*
74+
* * If `$promise` fulfills, and `$onFulfilledOrRejected` returns successfully,
75+
* `$newPromise` will fulfill with the same value as `$promise`.
76+
* * If `$promise` fulfills, and `$onFulfilledOrRejected` throws or returns a
77+
* rejected promise, `$newPromise` will reject with the thrown exception or
78+
* rejected promise's reason.
79+
* * If `$promise` rejects, and `$onFulfilledOrRejected` returns successfully,
80+
* `$newPromise` will reject with the same reason as `$promise`.
81+
* * If `$promise` rejects, and `$onFulfilledOrRejected` throws or returns a
82+
* rejected promise, `$newPromise` will reject with the thrown exception or
83+
* rejected promise's reason.
84+
*
85+
* `always()` behaves similarly to the synchronous finally statement. When combined
86+
* with `otherwise()`, `always()` allows you to write code that is similar to the familiar
87+
* synchronous catch/finally pair.
88+
*
89+
* Consider the following synchronous code:
90+
*
91+
* ```php
92+
* try {
93+
* return doSomething();
94+
* } catch(\Exception $e) {
95+
* return handleError($e);
96+
* } finally {
97+
* cleanup();
98+
* }
99+
* ```
100+
*
101+
* Similar asynchronous code (with `doSomething()` that returns a promise) can be
102+
* written:
103+
*
104+
* ```php
105+
* return doSomething()
106+
* ->otherwise('handleError')
107+
* ->always('cleanup');
108+
* ```
109+
*
110+
* @param callable $onFulfilledOrRejected
23111
* @return PromiseInterface
24112
*/
25113
public function always(callable $onFulfilledOrRejected);
26114

27115
/**
116+
* The `cancel()` method notifies the creator of the promise that there is no
117+
* further interest in the results of the operation.
118+
*
119+
* Once a promise is settled (either fulfilled or rejected), calling `cancel()` on
120+
* a promise has no effect.
121+
*
28122
* @return void
29123
*/
30124
public function cancel();

src/PromisorInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
interface PromisorInterface
66
{
77
/**
8+
* Returns the promise of the deferred.
9+
*
810
* @return PromiseInterface
911
*/
1012
public function promise();

src/functions.php

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@
22

33
namespace 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+
*/
519
function 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+
*/
2656
function 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+
*/
3776
function 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+
*/
4493
function 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+
*/
62125
function 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+
*/
70151
function 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+
*/
133225
function 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+
*/
165268
function reduce(array $promisesOrValues, callable $reduceFunc, $initialValue = null)
166269
{
167270
$cancellationQueue = new Internal\CancellationQueue();

0 commit comments

Comments
 (0)