复习之async

时间:2021-08-22 15:17:25   收藏:0   阅读:27
async
  其实返回了一个promise
  函数的返回值是promise的成功的结果,如果失败了,函数没有返回值就不会打印,什么都没有打印
  function aa() {
            return new Promise((resolve, reject) => {
                setTimeout(() => {
                    const num = parseInt(Math.random() * 100);
                    if (num % 2) {
                        resolve(num)
                    } else {
                        reject(num)
                    }
                }, 1000)
            })
        }



        async function fn() {
            // await 等待  
            let res = await aa();
            console.log(res);  //promise
            return res
        }

        const result = fn();
        console.log(result);  //成功的结果

 

技术分享图片

 

 技术分享图片

 

 

原文:https://www.cnblogs.com/hu-yan-123/p/15172057.html

评论(0)
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!