Js.Promise.AsyncAwait

Sugar for promises

let doSmth = () => {
  return new Promise((resolve, reject) => {    
    if (Math.random() > 0.5) {
      resolve('doSmth success');    
    }
    else
    {
      reject('doSmth Fail');    
    } 
  });    
}

// -- CALL OUR PROMISE 

const waitFunc = async () => {
    
    try {
        let resSmth = await doSmth();
        console.log(resSmth);
    } catch (error) {
        console.log(error);      
    }     
}

waitFunc();

// -- OR CALL IT ANONYMOUSLY

(async () => {
    try {
        let resSmth = await doSmth();
        console.log(resSmth);
    } catch (error) {
        console.log(error);      
    }      
})();
This entry was posted in Без рубрики. Bookmark the permalink.