简体   繁体   English

承诺要进行ES6重组?

[英]Promises with ES6 destructuring?

I've tried mixing Promises with ES6 destructuring syntax (just for experimentation purposes) but the following code throws an error: 我尝试将Promises与ES6解构语法混合(仅出于实验目的),但是以下代码引发错误:

function delay(ms) {
  return new Promise((resolve, reject) => {
    setTimeout(() => resolve(), ms)
  })
}

const { then } = delay(2000)

console.log(typeof then) // ==> 'function'

// throws TypeError:
then(() => {
  console.log(`done!`)
})

On Node.js v7.10.1 it prints: 在Node.js v7.10.1上打印:

TypeError: Cannot read property 'Symbol(promise_state_symbol)' of undefined TypeError:无法读取未定义的属性'Symbol(promise_state_symbol)'

Chrome console also throws a TypeError , but with a different message: Chrome控制台还会引发TypeError ,但消息不同:

Uncaught TypeError: Method Promise.prototype.then called on incompatible receiver undefined Uncaught TypeError:方法Promise.prototype.then在不兼容的接收器上调用undefined

These errors don't say much to me. 这些错误对我说的并不多。 What's better explanation for this? 有什么更好的解释呢?

It means that then is a method and you did not call it on any instance - you just called it as a function without any this context (or "receiver", as the second error message names it properly). 这意味着, then是方法,你没有把它在任何情况下-你说这就是因为没有任何一个功能this方面 (或“接收器”,作为第二个错误消息,将其命名为正常)。 You essentially did 您基本上是

const then = Promise.prototype.then
console.log(typeof then) // ==> 'function'
then(() => {}) // throws TypeError

You could use call 您可以使用call

const promise = delay(2000);
then.call(promise, console.log);

or just properly invoke promise.then(console.log) . 或者只是正确调用promise.then(console.log)

You are Assigning the then method to a variable, but then accesses this . 您正在将then方法分配给变量, then访问this You could use bind to achieve what you want. 您可以使用bind实现您想要的。

Basically methods in javascript are just functions that are using this . 基本上,javascript中的方法只是使用this函数。 If you are “stealing” the function and don't supply a this value, you are in dangerous territory. 如果您在“窃取”该功能并且不提供该值,那么您将处于危险境地。

Also the then you're extracting is most likely from Promise.prototype , not a function specific to the delay function. 另外, then你提取是最有可能从Promise.prototype ,不特定的延时功能的函数。

You just found a fancy way to get the method from the object. 您刚刚找到了从对象中获取方法的绝佳方法。 It has not much to do with destucturing at all… 完全与解构无关...

let p;
const {then} = p = delay(2000);
const then1 = p.then;

console.assert(then === Promise.prototype.then)
console.assert(then1 === then, 'thens aren\'t the same')

But you want a then , that somehow ensures, that you call it on the right promise. 但是你想then ,以某种方式确保,那你怎么称呼它右边的承诺。

So you could either go with 所以你可以选择

const p = delay(2000);
const then = p.then.bind(p);
…

Or construct another anonymous function 或构造另一个匿名函数

const p = delay(2000);
const then = fn => p.then(fn)

Note that this is not what you want, because it start the timeout when you are calling your then . 请注意,这不是您想要的,因为它在您调用then时开始超时。

const then = fn => delay(2000).then(fn) // ⚠ setTimeout gets called when you are calling then.

I see no way how you would achieve what you want in one line, but maybe others have an idea. 我看不到您如何在一线达成目标,但是其他人可能有一个主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM