What is the output of the following code? const p1 = Promise.resolve(1); const p2 = p1.then(v => { throw new Error(‘oops’); }); const p3 = p2.catch(e => e.message); const p4 = p3.then(v => console.log(v)); // What logs?

JavaScript Professional Hard

JavaScript Professional — Hard

What is the output of the following code? const p1 = Promise.resolve(1); const p2 = p1.then(v => { throw new Error(‘oops’); }); const p3 = p2.catch(e => e.message); const p4 = p3.then(v => console.log(v)); // What logs?

Key points

  • The error message 'oops' is passed through the promise chain.
  • The catch method handles the error and returns the message.
  • The console.log statement in p4 outputs the error message.

Ready to go further?

Related questions