Difference between promise and async await in Node.js - GeeksforGeeks

18 Apr, 2022
There are different ways to handle operations in NodeJS or in JavaScript. For asynchronous execution, different processes run simultaneously and are handled once the result of each process is available. There are different ways to handle the asynchronous code in NodeJS or in JavaScript which are:
Callbacks
Promises
Async/Await
1. Promises:
A Promise in NodeJS is similar to a promise in real life. It is an assurance that something will be done. Promise is used to keep track of whether the asynchronous event has been executed or not and determines what happens after the event has occurred. It is an object having 3 states namely:
Pending: Initial State, before the event has happened.
Resolved: After the operation completed successfully.
Rejected: If the operation had error during execution, the promise fails.
Example: While requesting data from a database server, the Promise is in a pending state until the data is received. If the data is received successfully, then the Promise is in resolved state and if the data could not be received successfully, the Promise is in the rejected state. 
Error Handling of Promises: For a successfully resolved promise, we use .then() method and for rejected promise, we use .catch() method. To run a code after the promise has been handled using .then() or .catch() method, we can .finally() method. The code inside .finally() method runs once regardless of the state of the promise.
Example:
Javascript
<script>  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");    });</script>
Output:
Promise resolved successfully
2. Async/Await:
Async/Await is used to work with promises in asynchronous functions. It is basically syntactic sugar for promises. It is just a wrapper to restyle code and make promises easier to read and use. It makes asynchronous code look more like synchronous/procedural code, which is easier to understand.
await can only be used in async functions. It is used for calling an async function and waits for it to resolve or reject. await blocks the execution of the code within the async function in which it is located. 
Error Handling in Async/Await: For a successfully resolved promise, we use try and for rejected promise, we use catch. To run a code after the promise has been handled using try or catch, we can .finally() method. The code inside .finally() method runs once regardless of the state of the promise.
Example:
Javascript
<script>  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();</script>
Output:
Strings are same
Difference Between Promise and Async/Await:
Promise
Async/Await
sumitgumber28
NodeJS-Questions
Picked
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Node.js fs.writeFile() Method
Node.js fs.readFile() Method
How to install the previous version of node.js and npm ?
How to use an ES6 import in Node.js?
How to read and write Excel file in Node.js ?
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?
Difference between var, let and const keywords in JavaScript