You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Dailoge, wulichenyang, Fleeting198, pandashuai, yanranjun and 52 moredyj7, lyhzs, Sun-Y0n9, AlexZhong22c, Machock and 4 moresheng-sndzhuyuesen, Dailoge, horseYao, voosil, yhwork and 1 morehuangpengkai, ViolaSong123, prince12138 and TubeTexashorseYao and tacindustryhorseYao, huangpengkai, wangjinshen, siyunlong-syl and razzh7artiely, blly5, chaofan-rensheng, GuokrSun, MaySept and 29 more
webwangshuai, jack-pearson, chaoranfeng, adminlly, huxiamei and 34 moregitlwz and Dexluxsheng-sndstingys, zuollly and heboliufengjiea879475908123 and zj251siyunlong-syl, a879475908123 and DULANGHAI
Activity
kunish commentedon Mar 7, 2019
这里对上面的例子做一个扩展
执行结果: 124536
LwDsmile commentedon Mar 12, 2019
学习了
KaiOrange commentedon Mar 14, 2019
Promise new的时候会立即执行里面的代码 then是微任务 会在本次任务执行完的时候执行 setTimeout是宏任务 会在下次任务执行的时候执行
pioneer26 commentedon Mar 27, 2019
excutor执行器里面是同步执行的,then里面是异步操作
yu910709 commentedon Apr 2, 2019
请问为什么3在val之前?
cliYao commentedon Apr 17, 2019
3是在5后面打印出来的啊,第一轮事件循环的时候,microtask queue里面先添加的promise.resolve(5).then((val)=>{console.log(val)}),后添加的promise.then(() => {
console.log(3);
});
[-]关于第13题的见解[/-][+]第 13 题:Promise 构造函数是同步执行还是异步执行,那么 then 方法呢?[/+]sunas commentedon Jul 9, 2019
Promise必然是同步的。就then我补充一下:
在ES6时代有了微异步的设定,then作为最典型代表,算是异步的一员。
在ES5时代,实现then的方式则要看构造函数里resolve(或reject)的用法了,如果resolve被同步使用,实质上resolve仍然是同步的。
wuzhong1030 commentedon Jul 10, 2019
Promise 构造函数是同步执行,then 是异步执行。看一下 Promise 的实现就知道了。
zhukunpenglinyutong commentedon Jul 11, 2019
看过 Event Loop 基础原理的就明白,Promise构造函数是同步执行,而 .then .catch .啥啥的是异步(还有process.nextTick等等,大家可以查),
而且放到了微队列中,async/await 中,await 前面的是同步,await 后面的是异步,写法上是这样,但是其实是 语法糖,最后还会转为 Promise.then的形式
Hunterang commentedon Jul 11, 2019
.then()当然是同步执行,只不过是.then的cb被放入了微任务队列,产生了异步执行
iamwelk commentedon Jul 13, 2019
这个题和之前的第8题类似
june38liu commentedon Jul 22, 2019
then()到底是同步执行还是异步执行?为什么回答里有的说是同步有的说是异步
thinkfish commentedon Jul 22, 2019
根据上面的回答,总结下应该是then函数本身是同步,then里面的cb是异步
17 remaining items
fengmiaosen commentedon Jul 6, 2020
更详细一点的说法是等到 promise变为 resolved状态的时候,then注册的回调函数才被放入到微任务队列中,等待调用执行
slogeor commentedon Jul 12, 2020
补段代码
tjwyz commentedon Jul 16, 2020
做戏做全套... 写一个更有助于理解
ps: 微任务以宏任务(setTimeout)替代
MrLeihe commentedon Apr 14, 2021
promise 的构造函数是同步执行的,then方法也是同步执行的,只不过 then 里面的回调函数是异步执行的。
TenviLi commentedon Apr 26, 2021
⚠ 同志们,这道题绝对没有你们想象的这么简单,补充一个不错的文章中的摘录:
leeFengHuo commentedon May 26, 2021
const promise = new Promise(async (resolve, reject) => {
console.log(1)
await resolve()
console.log(2)
})
promise.then(() => {
console.log(3)
})
console.log(4);
碰到过这种扩展,想了解为什么2在3之前执行
Chorer commentedon Aug 6, 2021
这种出现 await 的代码,你可以等价转化为:
也就是将 await 后面跟着的部分用一个
Promise.resolve()
包裹起来,然后剩余部分塞到一个 then 的回调函数里。这样再分析就简单了。gromy92 commentedon Mar 4, 2022
严格来说then方法 是同步执行的,then方法的参数(回调函数)是 异步执行的
xuchao996 commentedon Mar 8, 2022
如何体现呢?能举例说明吗?
yumengbdw commentedon Mar 17, 2022
之所以then是异步是为了处理循环引用的问题。
判断then里面new的promise 跟successCallback执行后得到的p1是不是同一个promise。
但是then里面处理回调的时候new的promise还没有创建完成。
Yuweiai commentedon Jul 4, 2022
【resolve对比await】这里虽然有resolve,但会打印同时先后打印1/2,如果是await,会先执行await后面的表达式,然后将其后的代码加入到微任务队列
WangYunFei1 commentedon Jul 13, 2022
promise是同步执行的,then也是同步的,只是then中的回调时异步执行的
Yangfan2016 commentedon Aug 23, 2022
可以看 promise的 实现
resolve 回调里的内容 会放入 微任务中
then 的回调只是一个list push 动作,
等 resolve 里的 微任务 执行完,就会 调用 then 里的 回调
so 构造器里除了 resolve 之外的 都是 同步执行的(await resolve() 这种情况除外)
then 里的回调是异步执行的
beinian717 commentedon Oct 8, 2024
请问有完整代码吗?
cliYao commentedon Oct 8, 2024