How to use Async-Await pattern in example in Node.js ? Last Updated : 23 May, 2022 Comments Improve Suggest changes Like Article Like Report 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. To use async-await, we just need to create an async function in which we will implement our try-catch block. In the try block, we will await our promise to be completed. If it gets resolved we will get the result otherwise an error will be thrown through the catch block. Await can only be used inside an async function or async callback or async arrow function. Example: JavaScript <script> function makeConnection(request) { // Promise resolves if connection is // made to GFG else it is rejected return new Promise((resolve, reject) => { if (request == 'GFG') resolve('Connected :)'); else reject('Connection failed :('); }); } async function doStuff(request) { try { const response = await makeConnection(request); console.log(response); } catch (err) { console.log(err); } } // Makes connection request to GFG doStuff('GFG'); // Makes connection request to Google doStuff('Google'); </script> Output: Connected :) Connection failed :( Comment More infoAdvertise with us Next Article How to use Async-Await pattern in example in Node.js ? ishankhandelwals Follow Improve Article Tags : Web Technologies Node.js NodeJS-Questions Similar Reads 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 How to use async/await with forEach loop in JavaScript ? Asynchronous is popular nowadays because it gives functionality of allowing multiple tasks to be executed at the same time (simultaneously) which helps to increase the productivity and efficiency of code. Async/await is used to write asynchronous code. In JavaScript, we use the looping technique to 2 min read How to use Async/Await with a Promise in TypeScript ? In TypeScript, you can use async and await with a Promise to simplify asynchronous code and make it more readable.What is a Promise?A promise in TypeScript is an object representing the eventual completion or failure of an asynchronous operation. It acts as a placeholder for a value that may be avai 3 min read How to Rewrite promise-based applications to Async/Await in Node.js ? In this article, we are going to see how we can rewrite the promise-based Node.js application to Async/Await. Suppose we have to read the content of a file 'index.txt', and we have a function 'readFilePromise' which uses the 'readFile' method of the fs module to read the data of a file, and the'read 2 min read Explain Promise.race() with async-await in JavaScript In this article, we will try to understand how we may implement Promise.race() method with async-await in JavaScript with the help of certain coding examples as well as theoretical explanations. Let us first quickly understand how we may implement Promise.race() method. This method is one of the mos 3 min read Difference between Promise and Async/Await in Node 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. PromiseA Promise is 4 min read How to Push Data to Array Asynchronously & Save It in Node.js ? Node.js is a powerful environment for building scalable and efficient applications, and handling asynchronous operations is a key part of its design. A common requirement is to collect data asynchronously and store it in an array, followed by saving this data persistently, for instance, in a databas 7 min read How to use await outside of an async function in JavaScript ? In this article, we will try to understand in what way or by how we may use await outside of an async function in JavaScript with the help of both theoretical explanations as well as coding examples. Let us first understand the following shown section in which all the syntaxes of declaring a promise 4 min read How to handle asynchronous operations in Node ? NodeJS, renowned for its asynchronous and event-driven architecture, offers powerful mechanisms for handling asynchronous operations efficiently. Understanding how to manage asynchronous operations is crucial for NodeJS developers to build responsive and scalable applications. What are Asynchronous 2 min read How to delay a loop in JavaScript using async/await with Promise ? In JavaScript, you can delay a loop by using async/await with Promise. By wrapping a setTimeout() inside a Promise and using await, you can pause execution at each iteration, creating delays between loop iterations for asynchronous tasks without blocking the main thread.What is async and await?async 2 min read Like