What is the difference between callback function and promise in Node.js?

1 Answers
Answered by suresh

Node.js Interview Question: Difference between callback function and promise

Node.js Interview Question: Difference between callback function and promise

In Node.js, callback functions and promises are commonly used to handle asynchronous operations. Here is the difference between them:

Callback Function:

A callback function is a function that is passed as an argument to another function and will be executed after the completion of an asynchronous operation. Callback functions can lead to callback hell or nested callbacks, making the code harder to read and maintain.

Promise:

A promise is an object that represents the eventual completion or failure of an asynchronous operation and allows for better handling of asynchronous code. Promises can be chained and provide methods like .then() and .catch() to handle success and error responses respectively.

So, while callback functions are traditional and can lead to callback hell, promises provide a cleaner and more organized way to handle asynchronous operations in Node.js.

Answer for Question: What is the difference between callback function and promise in Node.js?