Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. 'use strict';
  2.  
  3.  
  4. const willResolve = (value = true) => Promise.resolve(value);
  5.  
  6. const willReject = (msg = 'This is fucked up!') => Promise.reject(new Error(msg));
  7.  
  8. const toBeOrNotToBe = (value = true, msg = 'This may resolve... Or not') => {
  9. (Math.random() > 0.5) ? Promise.resolve(value) : Promise.reject(msg);
  10. };
  11.  
  12. // Anidated promise doesn't get catched by outer `catch()`. The next message will appear at the end:
  13. // UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 4): Error: This is fucked up!
  14. willResolve().then(() => {
  15. willReject().then(() => console.log('reached reject'));
  16. })
  17. .catch((error) => console.log('Outter catcher:', error));
  18.  
  19.  
  20. // An inner `catch()` is needed
  21. willResolve().then(() => {
  22. willReject().then(() => console.log('reached reject'))
  23. .catch((error) => console.log('Anidated catch:', error));
  24. })
  25. .catch((error) => console.log(error));
  26.  
  27. // Or better...
  28. willResolve().then(() => {
  29. return willReject().then(() => console.log(`This code won't run.`));
  30. })
  31. .catch((error) => console.log(`Now we're catching everything`, error));
  32.  
  33.  
  34. // in case you wanna play in the node console
  35. module.exports = {
  36. willResolve,
  37. willReject,
  38. toBeOrNotToBe,
  39. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement