How do JavaScript Promises differ from async/await?

Instruction: Describe the differences and similarities between Promises and async/await in JavaScript.

Context: This question tests the candidate's knowledge of asynchronous programming patterns in JavaScript, highlighting the evolution from Promises to async/await syntax.

Updated

Official answer available

Read the opening below, then unlock the full answer and practical guidance.

A Promise represents eventual fulfillment or rejection. then/catch attach reactions and return new promises for chaining. An async function always returns a promise; await expresses the same kind of asynchronous dependency using sequential-looking control flow.

function doubledWithThen(value) { return Promise.resolve(value).then(x => x * 2); } async function doubledWithAwait(value) { const x = await value; return x * 2; } Promise.all([doubledWithThen(3), doubledWithAwait(3)]) .then(values => console.log(values)); // [6, 6]...

Related Questions