JS.Promise.AsyncAwaitChain

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

let doSmthElse = async () => {
    return await new Promise((resolve, reject) => {
        if (Math.random() > 0.5) {
            resolve('doSmthElseSuccess')    
          }
          else
          {
            reject('doSmthElseFail');    
          }         

    })
}

// -- CHAIN

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