Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- const willResolve = (value = true) => Promise.resolve(value);
- const willReject = (msg = 'This is fucked up!') => Promise.reject(new Error(msg));
- const toBeOrNotToBe = (value = true, msg = 'This may resolve... Or not') => {
- (Math.random() > 0.5) ? Promise.resolve(value) : Promise.reject(msg);
- };
- // Anidated promise doesn't get catched by outer `catch()`. The next message will appear at the end:
- // UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 4): Error: This is fucked up!
- willResolve().then(() => {
- willReject().then(() => console.log('reached reject'));
- })
- .catch((error) => console.log('Outter catcher:', error));
- // An inner `catch()` is needed
- willResolve().then(() => {
- willReject().then(() => console.log('reached reject'))
- .catch((error) => console.log('Anidated catch:', error));
- })
- .catch((error) => console.log(error));
- // Or better...
- willResolve().then(() => {
- return willReject().then(() => console.log(`This code won't run.`));
- })
- .catch((error) => console.log(`Now we're catching everything`, error));
- // in case you wanna play in the node console
- module.exports = {
- willResolve,
- willReject,
- toBeOrNotToBe,
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement