AppDividend
Latest Code Tutorials

Javascript ES7 – Async / Await Tutorial With Example

Async / Await In Javascript

814

Javascript ES7 – Async / Await Tutorial With Example From Scratch is today’s leading topic. Node.js is now supporting Async-Await out of the box. Async/Await is a just new technique to write an async code in a sync function manner. You can write it as a simple synchronous function. That makes it very popular and understandable. Async-Await is a new way to write asynchronous code. Previous options for asynchronous code are callbacks and promises. Async/await built on top of promises. It cannot be used with plain callbacks or node callbacks.

Prerequisites

Promises In ES6

Callback in Javascript.

Javascript ES7 – Async / Await

Our function has the keyword async before it. The await keyword can only be used inside functions defined with asyncAny async function returns a promise implicitly, and the resolution value of the promise will be whatever you return from the function.

Related Posts
1 of 5
// server.js

async function Crypto() {
   return 10000;
 }
 
Crypto().then(BTC => {
   console.log(BTC)
});

In above example, It is quite clear that async function will always return a promise and if you have noted here that I have not used await yet.

Await function will always work inside the Async function. If you write await in the normal function then, it will give us an error. The keyword await JavaScript wait until that promise settles and returns its result. So it will pause until we can get our promise resolve and returns the resolved value.

Class Methods as async

A class method can also be async, just put async before it.

// server.js

class Crypto {
   async getCoin() {
     return await Promise.resolve(10000);
   }
 }
 
 new Crypto()
   .getCoin()
   .then(BTC => {console.log(BTC)});

Error handling

We can catch that error using try..catch, the same way as a regular throw.

// server.js

const fetch = require('node-fetch');

async function Crypto() {

   try {
     let response = await fetch('http://gibberissssh.com');
   } catch(err) {
     console.log(err);
   }
 }
 
 Crypto();

Switching from Promises to Async/Await.

We can use async/await by the following way.

function doubleAfter2Seconds(x) {
   return new Promise(resolve => {
     setTimeout(() => {
       resolve(x * 5);
     }, 2000);
   });
 }
 
 async function addAsync(x) {
   const a = await doubleAfter2Seconds(10);
   const b = await doubleAfter2Seconds(20);
   const c = await doubleAfter2Seconds(30);
   return x + a + b + c;
 }
 
 
 addAsync(100).then((sum) => {
   console.log(sum); //400
 });

In above code, first, we have written the doubleAfter2Seconds() this function, which returns a promise. Then we have called this function in the async function and waiting until it resolves. Then it will return a sum of that function. Finally!! You have just created an asynchronous function in JavaScript.

Leave A Reply

Your email address will not be published.