Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

宏任务微任务题 #44

Open
yiqia opened this issue Jun 8, 2021 · 6 comments
Open

宏任务微任务题 #44

yiqia opened this issue Jun 8, 2021 · 6 comments

Comments

@yiqia
Copy link

yiqia commented Jun 8, 2021

async function async1() {
console.log('async1 start')
await async2().then(res=>console.log(res))
console.log('async1 end')
}
async function async2() {
console.log('async2')
return 123
}
console.log('script start')
setTimeout(function () {
console.log('setTimeout')
}, 0)
async1()
new Promise(function (resolve) {
console.log('promise1')
resolve()
}).then(function () {
console.log('promise2')
})
console.log('script end')

打印的结果以及为什么顺序是这样

@ainuo5213
Copy link

/**

  • script start 宏任务队列:setTimeout
  • async1 start 微任务队列:123
  • async2 微任务队列:123,async1 end,await执行完之后会将之后的代码放进微任务队列(可以理解为回调)
  • promise1 微任务队列:promise2
  • script end 开始执行微任务队列里的任务
  • 123
  • promise2
  • async1 end 开始执行宏任务队列里的任务
  • setTimeout

*/

@aotemj
Copy link

aotemj commented Apr 7, 2022

async function async1() {
    console.log('async1 start');
    await async2().then(res => console.log(res));
    console.log('async1 end');
}

async function async2() {
    console.log('async2');
    return 123;
}

console.log('script start');

setTimeout(function () {
    console.log('setTimeout');
}, 0);

async1();

new Promise(function (resolve) {
    console.log('promise1');
    resolve();
}).then(function () {
    console.log('promise2');
});

console.log('script end');

打印的结果以及为什么顺序是这样

@aotemj
Copy link

aotemj commented Apr 7, 2022

建议添加一下代码包裹,方便读者阅读 @yiqia

1 similar comment
@aotemj
Copy link

aotemj commented Apr 7, 2022

建议添加一下代码包裹,方便读者阅读 @yiqia

@GdgmPhs
Copy link

GdgmPhs commented Aug 4, 2022

不太理解async1 end比promise 2先放进微队列,为啥promise 2先输出

@1529829142
Copy link

1529829142 commented Sep 13, 2022

@GdgmPhs 你可以这么理解,await 语句创建并返回了一个promise,await 后面的代码相当于then()的回调函数

async function async1() {
  console.log('2 async1 start');
  // await async2().then(res => console.log(res));
  // console.log('async1 end');
  // 相当于以下代码
  new Promise((resolve) => {
    async2()
    resolve(123)
  }).then((res) => {
    console.log('6 ' + res)
  })
  .then(() => {
    console.log('8 async1 end')
  })
}

async function async2() {
  console.log('3 async2');
  return 123;
}

console.log('1 script start');

setTimeout(function () {
  console.log('9 setTimeout');
}, 0);

async1();

new Promise(function (resolve) {
  console.log('4 promise1');
  resolve();
}).then(function () {
  console.log('7 promise2');
});

console.log('5 script end');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants