复习之async

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);  //成功的结果

 

 

 

 

 

相关文章

最后的控制台返回空数组.控制台在ids.map函数完成之前运行va...
我正在尝试将rxJava与我已经知道的内容联系起来,特别是来自J...
config.jsconstconfig={base_url_api:"https://douban....
我正在阅读MDN中的javascript,并且遇到了这个谈论承诺并且不...
config.jsconstconfig={base_url_api:"https://douban....
这是我的代码main.cpp:#include<string>#include<...