gpt4 book ai didi

javascript - resolve(thenable) 和 resolve ('non-thenable-object' ) 之间有什么区别?

转载 作者:数据小太阳 更新时间:2023-10-29 05:27:51 25 4
gpt4 key购买 nike

我试图理解 resolve(thenable)resolve('non-thenable-object') 之间的区别。

在下面的示例中,使用 promise 而不是 thenable,因为 promise 也是 thenable 并且可能更容易理解。

Demo1: resolve(promise)

let resolvePromise = new Promise(resolve => {
let resolvedPromise = Promise.resolve()
resolve(resolvedPromise)
})
resolvePromise.then(() => {
console.log('resolvePromise resolved')
})
let resolvedPromiseThen = Promise.resolve().then(res => {
console.log('promise1')
})
resolvedPromiseThen
.then(() => {
console.log('promise2')
})
.then(() => {
console.log('promise3')
})

结果:

  • promise 1
  • promise 2
  • resolvePromise 已解决
  • promise 3

Demo2: resolve('non-thenable-object')

let resolvePromise = new Promise(resolve => {
resolve('non-thenable-object')
})
resolvePromise.then(() => {
console.log('resolvePromise resolved')
})
let resolvedPromiseThen = Promise.resolve().then(res => {
console.log('promise1')
})
resolvedPromiseThen
.then(() => {
console.log('promise2')
})
.then(() => {
console.log('promise3')
})

结果:

  • resolvePromise 已解决
  • promise 1
  • promise 2
  • promise 3

所以,我检查了规范并找到了 Promise Resolve Functions.然后到了PromiseResolveThenableJobEnqueueJob .

所以,根据规范,我认为 demo1 就像

演示3:

let resolvePromise = new Promise(resolve => {
let resolvedPromise = Promise.resolve()
// resolve(resolvedPromise)
// works like
Promise.resolve().then(() => {
Promise.resolve(resolvedPromise).then(() => {
resolve()
})
})
})
resolvePromise.then(() => {
console.log('resolvePromise resolved')
})
let resolvedPromiseThen = Promise.resolve().then(res => {
console.log('promise1')
})
resolvedPromiseThen
.then(() => {
console.log('promise2')
})
.then(() => {
console.log('promise3')
})

我认为是因为Promise Resolve Functions说:

  1. Perform EnqueueJob("PromiseJobs", PromiseResolveThenableJob, « promise, resolution, thenAction »).

PromiseResolveThenableJob说:

This Job uses the supplied thenable and its then method to resolve the given promise. This process must take place as a Job to ensure that the evaluation of the then method occurs after evaluation of any surrounding code has completed.

此外,我认为 demo2 的工作方式类似于

演示4:

//let resolvePromise = new Promise(resolve => {
//resolve('str')
//})
//works like
let resolvePromise = Promise.resolve('str')

resolvePromise.then(() => {
console.log('resolvePromise resolved')
})
let resolvedPromiseThen = Promise.resolve().then(res => {
console.log('promise1')
})

resolvedPromiseThen
.then(() => {
console.log('promise2')
})
.then(() => {
console.log('promise3')
})

作为Promise Resolve Functions说:

If IsCallable(thenAction) is false, then Return FulfillPromise(promise, resolution).

虽然Demo1-Demo3Demo2-Demo4的结果是一样的,但我还是不确定如果我是对的。所以,我来问一下

我的逻辑是否正确?如果不是,你如何解释不同的命令 在 resolve(thenable)resolve(non-thenable) 之间?

最佳答案

是的,你的逻辑看起来是对的。

new Promise(resolve => resolve('non-thenable-object')) 等价于 Promise.resolve('non-thenable-object')所有目的。

不过,在您的 Demo3 中,我建议省略 Promise.resolve(resolvedPromise)。我不确定这是不是故意的,但是 Promise.resolve 确实有一个快捷方式,当它的参数已经是一个 promise 时,然后按原样返回 resolvedPromise .你宁愿写

new Promise((resolve, reject) => {
let resolvedPromise = Promise.resolve();
// resolve(resolvedPromise) works like
Promise.resolve().then(() => resolvedPromise.then(resolve, reject));
});

关于javascript - resolve(thenable) 和 resolve ('non-thenable-object' ) 之间有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53894038/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com