Errors and Promises

suggest change

Promises 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.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents