首页 > 试题广场 >

以下代码的运行结果是?const promiseA =...

[单选题]
以下代码的运行结果是?
const promiseA = Promise.resolve('a')
promiseA.then((res) => {
console.log(res)
}).then((res) => {
console.log(res)
})
const promiseB = Promise.resolve('b')
promiseB.then((res) => {
console.log(res)
})
promiseB.then((res) => {
console.log(res)
})
  • a b b undefined
  • a undefined b b
  • a b undefined undefined
  • a b a b
promise对象的第一个.then执行后,回调函数放入微任务中,从微任务中一次取出回调函数依次执行。反复循环。通俗来讲就是只有执行完所有的第一层.then里面的回调函数,才会开始执行第二层.then里面的回调函数;所以是a, b, b, undefined;因为promiseA第一层.then之后没有返回任何值。所以是的undefined
发表于 2022-08-15 17:34:31 回复(1)
看清楚下面两个 promiseB
宏任务,微任务,任务队列
发表于 2021-05-27 23:45:45 回复(0)
发表于 2023-09-09 14:15:11 回复(0)
首先两个promise。因为promise内部代码是同步执行。我们可以理解成先Apromise的resolve再到Bpromise的resolve。这两个不重要。重要的是.then的顺序。首先是Apromise的.then 再因为Bpromise的.then。因为有两个。也因为Apromise的.then是链式调用在第一个没有继续resolve的情况下所有是未定义
发表于 2021-06-16 08:20:35 回复(0)