logo CCISEL
  • Home
  • Team
  • Academia
  • News
  • Archive
  • Contacts
  

JavaScript known behavior or specification?

October 30, 2020 | Pedro Félix

What is the expected behavior from the following usage that manages two Promises (one rejected on creation and other fulfilled after a 100 milliseconds timeout), which results in another rejected Promise?

const f = async () => {
    const rej = Promise.reject(new Error("rej"));      // Rejected on creation
    const ful = new Promise(r => setTimeout(r, 1000)); // Fullfiled after 1000 ms
    await ful;                                         // Await for ful completion
    console.log('after await');                        
    await rej;                                           // Await for rej completion
}
f().catch((err) => {                        // On resulting Promise rejection then...
    console.log(`caught: ${err}`);
});

Copyright © CCISEL 2025