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

时间:2018-12-22 08:09:50

标签: javascript node.js promise

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

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

演示1: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')
  })

结果:

  • promise1
  • promise2
  • resolvePromise已解决
  • promise3

演示2: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已解决
  • promise1
  • promise2
  • promise3

因此,我检查了规格并发现Promise Resolve Functions 。然后进入PromiseResolveThenableJobEnqueueJob

因此,根据规范,我认为 demo1 就像

Demo3:

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. 执行EnqueueJob(“ PromiseJobs”,PromiseResolveThenableJob,«promise,resolution,thenAction»)。
  2.   

PromiseResolveThenableJob说:

  

此作业使用提供的thenable及其then方法来解决给定的承诺。 此过程必须作为作业进行,以确保对所有周围代码的评估都在完成后对then方法进行评估。

此外,我认为 demo2 的工作原理

Demo4:

//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所说:

  

如果IsCallable(thenAction)为false,则返回FulfillPromise(承诺,分辨率)。

尽管 Demo1 - Demo3 Demo2 - Demo4 之间的结果相同,但我仍不确定如果我是对的。所以,我在这里问

我的逻辑是否正确?如果没有,您如何解释不同的订单  在resolve(thenable)resolve(non-thenable)之间?

2 个答案:

答案 0 :(得分:2)

是的,您的逻辑看起来正确。

new Promise(resolve => resolve('non-thenable-object'))等效于Promise.resolve('non-thenable-object')

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

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

答案 1 :(得分:1)

在阅读了规范并进行了多次测试后,我认为我可能会得到它。

在开始之前,我们必须解决一些问题。

RESOLVE()中使用resolve时将其命名为Promise executor。例如,RESOLVE(thenable)表示如下代码:

  new Promise((resolve,reject)=>{
    resolve(thenable)
  })

resolve(thenable)的意思是Promise.resolve(thenable)

好,我们开始吧。

Promise.resolve('non-thenable')RESOLVE('non-thenable')

我们在使用Promise.resolve('non-thenable')时涉及Promise.resolve

enter image description here

然后是PromiseResolve

enter image description here

这就是Promise.resolve('non-thenable')转换为

的地方
new Promise(resolve=>{
  resolve('non-thenable')
})

因此,我们得出结论:

  

Promise.resolve('non-thenable')可以转换为RESOLVE('non-thenable')


RESOLVE(thenable)

demo1

let resolveThenable = new Promise((resolve, reject) => {
  let thenable = {
    then: function (resolve, reject) {
      console.log('in thenable')
      resolve(42)
    }
  }
  resolve(thenable)
  // works like
  // Promise.resolve().then(() => {
  //   thenable.then(resolve)
  // })
  // should be ?
  // Promise.resolve().then(() => {
  //   thenable.then.[[Value]](resolve)
  // })
  // equivalent to?
  // Promise.resolve().then(() => {
  //   thenable.then(resolve)
  // })
})
resolveThenable.then(() => {
  console.log('resolveThenable resolved')
})
let resolvedPromiseThen = Promise.resolve().then(res => {
  console.log('promise1')
})
resolvedPromiseThen
  .then(() => {
    console.log('promise2')
  })
  .then(() => {
    console.log('promise3')
  })

// 'in thenable'
// 'promise1'
// 'resolveThenable resolved'
// 'promise2'
// 'promise3'

根据Promise Resolve Functions,当我们使用RESOLVE(thenable)时涉及

enter image description here

然后是PromiseResolveThenableJob

enter image description here

这将使RESOLVE(thenable)的工作类似于

  Promise.resolve().then(() => {
    thenable.then.[[Value]](resolve)
  })

所以,我认为它等同于

  Promise.resolve().then(() => {
    thenable.then(resolve)
  })

RESOLVE(thenable)的结果相同。

  

因此,我们可以说RESOLVE(thenable)可以转换为

  Promise.resolve().then(() => {
    thenable.then(resolve)
  })
演示2

let resolvePromise = new Promise((resolve, reject) => {
  let resolvedPromise = Promise.resolve()
  resolve(resolvedPromise)
  // works like
  // Promise.resolve().then(() => {
  //   resolvedPromise.then(() => {
  //     resolve()
  //   })
  // })
  // should be?
  // Promise.resolve().then(() => {
  //   resolvedPromise.then.[[Value]](resolve,reject)
  // })
  // equivalent to ?
  // Promise.resolve().then(() => {
  //   resolvedPromise.then(resolve)
  // })
  // equivalent to ?
  // Promise.resolve().then(() => {
  //   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')
  })

// 'promise1'
// 'promise2'
// 'resolvePromise resolved'
// 'promise3'

当我们谈论RESOLVE(resolvedPromise)时,我们发现该规范并未将thenablepromise区分开。因此,以同样的方式,RESOLVE(resolvedPromise)可以转换为

  Promise.resolve().then(() => {
    resolvedPromise.then(resolve)
  })

尽管在这种情况下,RESOLVE(thenable)RESOLVE(promise)之间的顺序是不同的。因为thenable.then是同步操作,而resolvedPromise.then是异步操作。它们不是相同的then方法。

所以,这是我们的结论:

  

RESOLVE(thenable)RESOLVE(promise)都可以转换为

 new Promise((resolve, reject) => {
      Promise.resolve().then(() => {
        thenable.then(resolve)
      })
 })

Promise.resolve(可解决)

使用Promise.resolve(promise)时非常简单,因为它返回promise参数。

但是,使用Promise.resolve(thenable)会使事情变得复杂,而thenable并不是一个保证。我们称之为Promise.resolve(nonPromiseThenable)

根据Promise.resolve ( x )

enter image description here

然后是

enter image description here

因此,Promise.resolve(nonPromiseThenable)可以转换为

 new Promise(resolve => { 
   resolve(nonPromiseThenable)
 })

最后来到

 new Promise(resolve => { 
   Promise.resolve().then(() => { 
     nonPromiseThenable.then(resolve) 
   }) 
 })

您可以在下面的演示中对其进行测试。

var thenable = {
  then(resolve, reject) {
    resolve(1)
  }
}
// code transformation 
Promise.resolve(thenable).then(res => {
  console.log(res)
})
// equal 
// new Promise(resolve => { 
//   resolve(thenable) 
// }).then(res => { 
//   console.log(res) 
// }) 
// equal 
// new Promise(resolve => { 
//   Promise.resolve().then(() => { 
//     thenable.then(resolve) 
//   }) 
// }).then(res => { 
//   console.log(res) 
// }) 

new Promise(resolve => resolve(2))
  .then(res => {
    console.log(res)
  })
  .then(res => console.log(3))

最后,我们得出一个结论:

  • Promise.resolve('nonThenable')可以转换为RESOLVE('nonThenable')。它们具有相同的效果。
  • Promise.resolve(thenable)RESOLVE(thenable)不同。它们有不同的效果。
  • RESOLVE(thenable)RESOLVE(promise)可以转换为 new Promise((resolve, reject) => { Promise.resolve().then(() => { thenable.then(resolve) }) })
  • Promise.resolve(promise) === promisePromise.resolve(nonPromiseThenable)可以转换为 new Promise(resolve => { Promise.resolve().then(() => { nonPromiseThenable.then(resolve) }) })