Which of the following best explains why promises are used in JavaScript?
Think about how JavaScript handles tasks that take time, like loading data from the internet.
Promises help manage asynchronous tasks by allowing code to run after a task finishes, avoiding deeply nested callbacks.
What will be the output of the following JavaScript code?
Promise.resolve(5) .then(x => x * 2) .then(x => x + 3) .then(console.log);
Follow the value through each then step.
The promise starts with 5, then multiplies by 2 (10), then adds 3 (13), and finally logs 13.
What will this code print to the console?
Promise.reject('fail') .catch(err => 'recovered') .then(console.log);
Look at how the catch changes the flow.
The promise rejects with 'fail', but catch handles it and returns 'recovered', which is logged.
Which statement best describes how promises improve JavaScript code readability?
Think about how promises let you write steps one after another.
Promises let you chain asynchronous steps in a clear, linear way, avoiding nested callbacks.
What will be the output of this code?
Promise.all([ Promise.resolve(1), Promise.reject('error'), Promise.resolve(3) ]) .then(console.log) .catch(console.log);
Remember how Promise.all behaves when one promise rejects.
Promise.all rejects immediately if any promise rejects, so the catch logs 'error'.