Function
rethrow (reason) public
Defined in node_modules/rsvp/lib/rsvp/rethrow.js:1
- reason
- Error
- reason the promise became rejected.
rethrow
will rethrow an error on the next turn of the JavaScript event
loop in order to aid debugging.
Promises A+ specifies that any exceptions that occur with a promise must be
caught by the promises implementation and bubbled to the last handler. For
this reason, it is recommended that you always specify a second rejection
handler function to then
. However, rethrow
will throw the exception
outside of the promise, so it bubbles up to your console if in the browser,
or domain/cause uncaught exception in Node. rethrow
will also throw the
error again so the error can be handled by the promise per the spec.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import { rethrow } from 'rsvp'; function throws(){ throw new Error('Whoops!'); } let promise = new Promise(function(resolve, reject){ throws(); }); promise.catch(rethrow).then(function(){ // Code here doesn't run because the promise became rejected due to an // error! }, function (err){ // handle the error here }); |
The 'Whoops' error will be thrown on the next turn of the event loop
and you can watch for it in your console. You can also handle it using a
rejection handler given to .then
or .catch
on the returned promise.