How to use Async-Await pattern in example in Node.js ? Last Updated : 23 May, 2022 Suggest changes Share 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 :( Advertise with us Next Article How to use Async-Await pattern in example in Node.js ? I ishankhandelwals Follow 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 Article Tags : Web Technologies Node.js NodeJS-Questions Like