Skip to content

Commit c1aad8e

Browse files
committed
Recommend rejecting promises by throwing an exception
1 parent 26718a5 commit c1aad8e

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,14 +308,16 @@ $resolver = function (callable $resolve, callable $reject) {
308308
// resolve or reject.
309309

310310
$resolve($awesomeResult);
311+
// or throw new Exception('Promise rejected');
311312
// or $resolve($anotherPromise);
312313
// or $reject($nastyError);
313314
};
314315

315-
$canceller = function (callable $resolve, callable $reject) {
316+
$canceller = function () {
316317
// Cancel/abort any running operations like network connections, streams etc.
317318

318-
$reject(new \Exception('Promise cancelled'));
319+
// Reject promise by throwing an exception
320+
throw new Exception('Promise cancelled');
319321
};
320322

321323
$promise = new React\Promise\Promise($resolver, $canceller);
@@ -329,7 +331,8 @@ function which both will be called with 3 arguments:
329331
When called with a non-promise value, fulfills promise with that value.
330332
When called with another promise, e.g. `$resolve($otherPromise)`, promise's
331333
fate will be equivalent to that of `$otherPromise`.
332-
* `$reject($reason)` - Function that rejects the promise.
334+
* `$reject($reason)` - Function that rejects the promise. It is recommended to
335+
just throw an exception instead of using `$reject()`.
333336

334337
If the resolver or canceller throw an exception, the promise will be rejected
335338
with that thrown exception as the rejection reason.

0 commit comments

Comments
 (0)