-
Notifications
You must be signed in to change notification settings - Fork 643
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
编程题:以下输出顺序多少 (setTimeout 与 promise 顺序) #120
Comments
1 3 4 2 5 6 0 |
1 3 4 2 5 6 0 |
javascript明明是单线程同步语言,但是却能实现异步效果,什么原因呢? 更详细的了解宏微任务可以点这里 |
@qianbaiduhai |
/**
* 1. 分析以下程序段的输出顺序
* 主要是考察微任务 宏任务
*/
setTimeout(()=>console.log(0))
new Promise((resolve)=>{
console.log(1)
resolve(2)
console.log(3)
}).then(res=>console.log(res))
new Promise((resolve)=>{
console.log(4)
resolve(5)
}).then(res=>console.log(res)).then(()=>console.log(6))
console.log(8)
Promise.resolve(7).then(res=>console.log(res))
/**
* 分析:1 3 4 2 5 6 0
* 1. setTimeout放到宏任务队列
* 2. 立即执行new Promise 1 3 then放到微任务队列
* 3. 立即执行new Promise 4 then放到微任务队列
* 4. 循环微任务队列 2 5 6
* 5. 循环宏任务队列
*/ |
1 3 4 8 2 5 7 6 0 |
可参考 从一道面试题谈谈对 EventLoop 的理解
The text was updated successfully, but these errors were encountered: