DEV Community

Mahin Tazuar
Mahin Tazuar

Posted on

Basic Async Await

Async-await
when we need to work with multiple promise its very complex for handaling.And we need to use nested callback that is not easy to read and we need to use then for going the next step , That's situation called callback hell. But Async and await work more friendly , when ASYNC keyword use in a function its change the function behaviours and await control order multiple promise.If we are use async , await with promise we do not need to use then , we can fix and get the error message using with try catch syste.

var promise1 = new Promise((resolve, reject) => {
   setTimeout(() => {
      if(true) {
         resolve('Promise 1 Resolved');
      } else {
         reject('Promise 1 Error');
      }
   }, 5000)
 })

 async function hello(){
   var data = await promise1;
console.log(data);
}
hello();
Enter fullscreen mode Exit fullscreen mode

Latest comments (0)