为什么then(console.log(myVar))在函数体内的第一个awiat执行后被callback了

let myVar = ""

let asyncAccessURL = async function () {
    myVar = "landmark1"
    const f1 = await fetch('https://www.baidu.com');
    myVar = "landmark2"
    const f2 = await fetch('https://www.baidu.com');
    myVar = "landmark3"
    console.log(f1);
    console.log(f2);
};

asyncAccessURL().then(console.log(myVar))

image.png

阅读 1.3k
1 个回答

then接收的是一个回调函数作为参数,
then(res=>{})//是个未执行的函数。
你这么写then(console.log())//相当于把console.log()的返回结果(undefined)传给了then;
而console.log(myVar)在第一时间执行并输出在控制台了。

    asyncAccessURL().then(undefined);
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题