What are Promises in Node.js and how do they differ from callbacks?

Instruction: Explain the concept of Promises in Node.js and compare them with callback functions.

Context: This question assesses the candidate's understanding of asynchronous programming in Node.js, focusing on the use of Promises to manage asynchronous operations more effectively than callbacks.

Updated

Official answer available

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

A callback is a function supplied for another operation to invoke. Node APIs often use an error-first callback convention, but callbacks can also be synchronous or invoked repeatedly, depending on the API.

A Promise represents an eventual result with pending, fulfilled or rejected state. It settles only once, and then/catch handlers compose later work through returned promises. Throwing in a handler rejects the promise it returns; returning another promise connects the result to that operation...

Related Questions