Difference between Promise and Async/Await in Node
Last Updated :
04 Apr, 2025
In Node.js, both Promises and Async/Await are used for handling asynchronous operations, but they differ in how they simplify the code. Promises provide a simple way to work with callbacks, while Async/Await offers a more readable and synchronous way to handle asynchronous tasks.
Promise
A Promise is an object representing the eventual completion (or failure) of an asynchronous operation. It provides a cleaner way to handle asynchronous operations compared to traditional callback functions, helping to avoid "callback hell."
Key Characteristics of Promises
- States: A Promise can be in one of three states.
- Pending: Initial state, neither fulfilled nor rejected.
- Fulfilled: Operation completed successfully.
- Rejected: Operation failed.
- Methods:
- then(): Executes when the Promise is fulfilled.
- catch(): Executes when the Promise is rejected.
- finally(): Executes regardless of the Promise's outcome.
Error Handling of Promises
To handle the outcome of a Promise:
- Resolved Promise: Use the .then() method.
- Rejected Promise: Use the .catch() method.
- Regardless of Outcome: Use .finally() to execute code after handling the Promise.
Example: During database server data retrieval, a Promise remains pending until successful data reception (resolved state) or unsuccessful retrieval (rejected state).
JavaScript
const promise = new Promise(function (resolve, reject) {
const string1 = "geeksforgeeks";
const string2 = "geeksforgeeks";
if (string1 === string2) {
resolve();
} else {
reject();
}
});
promise
.then(function () {
console.log("Promise resolved successfully");
})
.catch(function () {
console.log("Promise is rejected");
});
Output:
Promise resolved successfully
Async/Await
Async/Await is a simpler way to work with Promises in JavaScript. It lets you write code that handles tasks which take time, like fetching data, in a way like regular, step-by-step code. The async keyword is used to create a function that returns a Promise, while the await keyword pauses the function's execution until the Promise is done.
Key Characteristics of async/await
- Async Functions: An async function returns a Promise. It allows the use of the await keyword inside it.
- Await: The await keyword pauses the execution of the async function until the Promise is resolved or rejected.
Error Handling in Async/Await:
Error handling is done using try and catch blocks, similar to synchronous code.
- Resolved Promise: Inside try.
- Rejected Promise: Inside catch.
- Regardless of Outcome: You can still use .finally() after try or catch.
Example:
JavaScript
const helperPromise = function () {
const promise = new Promise(function (resolve, reject) {
const x = "geeksforgeeks";
const y = "geeksforgeeks";
if (x === y) {
resolve("Strings are same");
} else {
reject("Strings are not same");
}
});
return promise;
};
async function demoPromise() {
try {
let message = await helperPromise();
console.log(message);
} catch (error) {
console.log("Error: " + error);
}
}
demoPromise();
Output:
Strings are same
Difference Between Promise and Async/Await
Below is a comparison between Promises and Async/Await:
Promise | Async/Await |
---|
A Promise represents an intermediate state of an operation and is guaranteed to complete its execution at some point in the future. | Async/Await is syntactic sugar for Promises, making asynchronous code behave more synchronously. |
Promise has 3 states - resolved, rejected and pending. | It does not have any states. It returns a promise either resolved or rejected. |
If the function "fxn1" is to executed after the promise, then promise.then(fxn1) continues execution of the current function after adding the fxn1 call to the callback chain. | If the function "fxn1" is to executed after await, then await X() suspends execution of the current function and then fxn1 is executed. |
Error handling is done using .then() and .catch() methods. | Error handling is done using .try() and .catch() methods. |
Promise chains can become difficult to understand sometimes. | Using Async/Await makes it easier to read and understand the flow of the program as compared to promise chains. |
Conclusion
Both Promises and Async/Await handle asynchronous operations in Node.js, but Async/Await offers a more readable, synchronous-like approach compared to Promises. Promises are useful for chaining .then() and .catch(), while Async/Await simplifies error handling and improves code readability.
Similar Reads
What is the Difference Between Promises and Observables in Angular ? Angular is a TypeScript-based Single-Page Application framework used to create dynamic and responsive web applications. It is one of the most popular and robust frameworks. One of its key features is to handle Asynchronous Operations effectively. In Asynchronous, code is not executed sequentially ra
5 min read
What is the difference between async.waterfall and async.series? async.waterfall: This waterfall method runs all the functions(i.e. tasks) one by one and passes the result of the first function to the second, second function's result to the third, and so on. When one function passes the error to its own callback, then the next functions are not executed. So, the
3 min read
Explain Async Await with Promises in Node.js Async/Await in Node.js is a powerful way to handle asynchronous operations. It allows you to write asynchronous code in a synchronous manner, making it easier to read, write, and debug. This feature is built on top of Promises, which represent a value that may be available now, or in the future, or
4 min read
What is the difference between await and yield keywords in JavaScript ? In this article, we will see how Await keyword is different from Yield keywords. Generator Functions: Functions that can return multiple values at different time interval as per the user demands, and can manage its internal state are generator functions. A function becomes a Generator function if it
3 min read
Difference between Synchronous and Asynchronous Method of fs Module Asynchronous fs methods in Node.js do not block the event loop and handle multiple operations concurrently, improving performance while Synchronous fs methods block the event loop until the operation completes, which can lead to inefficiencies and slower performance for I/O-bound tasks. Table of Con
5 min read
Difference between Asynchronous and Non-blocking Asynchronous and non-blocking are related but distinct concepts in programming, particularly in the context of I/O operations. Asynchronous: Asynchronous refers to the ability of a program or system to perform multiple tasks simultaneously without waiting for each task to be complete before starting
2 min read
Difference between readFile and createReadStream in Node.js Node.js offers various methods for file handling, two of the most commonly used being readFile and createReadStream. fs.readFile and fs.createReadStream are both used to read files in Node.js, but they cater to different needs. fs.readFile reads the entire file into memory, making it suitable for sm
5 min read
How to use Async-Await pattern in example in Node.js ? Modern javascript brings with it the async-await feature which enables developers to write asynchronous code in a way that looks and feels synchronous. This helps to remove many of the problems with nesting that promises have, and as a bonus can make asynchronous code much easier to read and write.
1 min read
Difference between Fetch and Axios for Making HTTP Requests Fetch and Axios are commonly used in web applications to make HTTP requests from the frontend to the backend. While Fetch is built into modern browsers, Axios is a popular library known for its simplicity and extra features. Both can handle the same tasks like sending and receiving data.What is Fetc
3 min read
How does await and async works in ES6 ? In this article, we will try to understand how exactly async and await works in ES6. Let us first try to understand what exactly async and await are along with their usage and then further we will see some examples in which we would see how we may use async and await along with the normal functions.
3 min read