Interaction with Promises

suggest change

Exceptions are to synchronous code what rejections are to promise-based asynchronous code. If an exception is thrown in a promise handler, that rejection replaces the thrown exception.

Promise.resolve(5)
    .then(result => {
        throw new Error("I don't like five");
    })
    .then(result => {
        console.info("Promise resolved: " + result);
    })
    .catch(error => {
        console.error("Promise rejected: " + error);
    });
Promise rejected: Error: I don't like five

async / await, standard since ECMAScript 2017, goes the other way. Awaiting a rejected promise throws:

async function main() {
  try {
    await Promise.reject(new Error("Invalid something"));
  } catch (error) {
    console.log("Caught error: " + error);
  }
}
main();
Caught error: Error: Invalid something

Feedback about page:

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



Table Of Contents