Promiseは両目がぼんやりしていて、両目に光を放つ(9)-asyncとawait


async関数
asyncのMDN文書
1. async       promise  
2. async     promise             
async function fn1() {
    return 1
}
console.log(fn1())      Promise  
async function fn1() {
    return 1  //   value 1 promise
    throw 2   //   reason 2 promise
    return Promise.reject(3) //   reason 3 promise
    return Promise.resolve(3)  //   reason 3 promise
    return new Promise((resolve, reject) => {
        resolve(4)  //   value 4 promise
        reject(4)   //   reason 4 promise
    }) 
  }
await表現
awaitのMDN文書
1. await         promise  ,          
2.       promise  , await    promise    
3.          ,        await    
4.   await promise   ,       ,     try...catch     
5. await    async   ,  async       await
function fn2() {  
  return new Promise((resolve, reject) => {  
    resolve(1)  
  })  
}  
  
function fn3() {  
  return 3  
}  
  
function fn4() {  
  return new Promise((resolve, reject) => {  
    reject(2)  
  })  
}  
  
 //     await,        async,  async       await
(async function(){  
  const r1 = await fn2()  
  console.log(r1)  //     1,    fn2     1,   fn2()       value 1 promise,await     promise    value
  
  const r2 = await fn3()  
  console.log(r2)  //     3,await        promise,      
  
  const r3 = await fn4()  
  console.log(r3)  //uncaught  ,  await          promise,     try...catch     
  try {  
    await fn4()  
  } catch(error) {  
    console.log(error)  //     2
  }
})()