Errors and Promises
suggest changePromises handle errors differently to synchronous or callback-driven code.
const p = new Promise((resolve, reject) => {
reject(new Error('Oops'));
});
// anything that is `reject`ed inside a promise will be available through catch
// while a promise is rejected, `.then` will not be called
p
.then(() => {
console.log("won't be called");
})
.catch(e => {
console.log(e.message);
})
// once the error is caught, execution flow resumes
.then(() => {
console.log('hello!');
});
Oops
hello!
An unhandled promise rejection terminates modern Node.js processes with an error, so every promise chain should handle its failures or return them to a caller that does.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents