求解一道promise笔试题

let promise1 = Promise.resolve()
    .then(res => console.log(1))
    .then(res => console.log(2))

let promise2 = new Promise(resolve => {
    setTimeout(() => {
        console.log(6)
        resolve()
    })
}).then(res => console.log(3))

async function main() {
    console.log(4)
    console.log(await Promise.all([promise2, promise1]))
    console.log(5)
    return { obj: 5 }
}

let promise3 = Promise.resolve()
    .then(res => console.log(8))
    .then(res => console.log(9))

console.log(typeof main())
碰到了一道前端promise的笔试题,有点绕,想不明白,有哪个大佬能讲解一下吗?🤣🤣

运行结果如下:

#笔试题目#
全部评论
由于字数限制……只能搞成图片了。然后还不知道我说的对不对…… 1、执行第一行 Promise.resolve() 微任务【 promise1的第一个then 】 2、执行第5行 new Promise(resolve => {     setTimeout(() => {         console.log(6)         resolve()     }) }) setTimeout加入到宏队列 宏队列【 L6:setTimeout 】 微任务【 promise1的第一个then 】 3、执行第19行 let promise3 = Promise.resolve() then加入微任务 宏队列【 L6:setTimeout 】 微任务【 promise1的第一个then、promise3的第一个then 】 4、执行main() 5、输出console.log(4) 当前输出【 4 】 6、碰到await,弹出执行栈,返回一个Promise对象 typeof Promise.all([]) == 'object&(10249)#39; 输出object 当前输出【 4 object 】 7、等待 Promise.all执行完毕再执行后面的    console.log(5)后面的代码 await下面的代码只有当await后面的promise返回结果后才可以执行 8、开始执行微任务 微任务【 promise1的第一个then、promise3的第一个then 】 9、执行promise1的第一个then 输出console.log(1) 当前输出【 4 object 1 】 宏队列【 L6:setTimeout 】 微任务【 promise3的第一个then、promise1的第二个then 】 10、执行promise3的第一个then 输出console.log(8) 当前输出【 4 object 1 8 】 宏队列【 L6:setTimeout 】 微任务【 promise1的第二个then、promise3的第二个then 】 11、执行promise1的第二个then 输出console.log(2) 当前输出【 4 object 1 8 2 】 宏队列【 L6:setTimeout 】 微任务【 promise3的第二个then 】
点赞 回复 分享
发布于 2020-11-17 16:01
这里object指的是async函数返回的promise对象啊,await 后面是resolve微任务异步执行。
点赞 回复 分享
发布于 2020-11-16 15:47
哪块不明白啊?
点赞 回复 分享
发布于 2020-11-15 21:57
抱歉,打扰了。
点赞 回复 分享
发布于 2020-11-15 21:20

相关推荐

07-17 11:56
门头沟学院 Java
感谢东子的收留
熬夜脱发码农:无敌了,这是我看到第二个京东的提前批大佬了我还在畏畏缩缩准备八股算法
点赞 评论 收藏
分享
zzzzhz:兄弟你先猛猛投简历至少三百家,能约到面试就去面。最近可以速成智能小车,智慧家居烂大街的项目,不需要自己写,只需要把里面的代码讲解看明白就行。把其中涉及到的八股文都拿出来单独背一下,我去年找工作就一个智能小车智慧家居找了10k差不多。
点赞 评论 收藏
分享
05-29 09:02
门头沟学院 Java
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-18 18:30
点赞 评论 收藏
分享
评论
点赞
2
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务