rethrow.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /**
  2. `RSVP.rethrow` will rethrow an error on the next turn of the JavaScript event
  3. loop in order to aid debugging.
  4. Promises A+ specifies that any exceptions that occur with a promise must be
  5. caught by the promises implementation and bubbled to the last handler. For
  6. this reason, it is recommended that you always specify a second rejection
  7. handler function to `then`. However, `RSVP.rethrow` will throw the exception
  8. outside of the promise, so it bubbles up to your console if in the browser,
  9. or domain/cause uncaught exception in Node. `rethrow` will also throw the
  10. error again so the error can be handled by the promise per the spec.
  11. ```javascript
  12. function throws(){
  13. throw new Error('Whoops!');
  14. }
  15. let promise = new RSVP.Promise(function(resolve, reject){
  16. throws();
  17. });
  18. promise.catch(RSVP.rethrow).then(function(){
  19. // Code here doesn't run because the promise became rejected due to an
  20. // error!
  21. }, function (err){
  22. // handle the error here
  23. });
  24. ```
  25. The 'Whoops' error will be thrown on the next turn of the event loop
  26. and you can watch for it in your console. You can also handle it using a
  27. rejection handler given to `.then` or `.catch` on the returned promise.
  28. @method rethrow
  29. @static
  30. @for RSVP
  31. @param {Error} reason reason the promise became rejected.
  32. @throws Error
  33. @static
  34. */
  35. export default function rethrow(reason) {
  36. setTimeout(() => {
  37. throw reason;
  38. });
  39. throw reason;
  40. }