简体   繁体   English

如何将'Promise.reject`视为一流的功能?

[英]How to treat `Promise.reject` as a first-class function?

Why can I do this: 我为什么这样做:

> Promise.reject(3);
< Promise {[[PromiseStatus]]: "rejected", [[PromiseValue]]: 3}

But not this: 但不是这个:

> var f = Promise.reject;
< undefined

> f(3)
< VM2366:1 Uncaught TypeError: PromiseReject called on non-object
    at reject (<anonymous>)
    at <anonymous>:1:1

The spec defines Promise.reject as follows (emphasis mine): 规范Promise.reject定义如下(强调我的):

25.4.4.4 Promise.reject( r ) 25.4.4.4 Promise.reject( r )

The reject function returns a new promise rejected with the passed argument. reject函数返回一个被传递的参数拒绝的新promise。

  1. Let C be the this value. C为该值。
  2. If Type( C ) is not Object, throw a TypeError exception. 如果Type( C )不是Object,则抛出TypeError异常。
  3. Let promiseCapability be ? promiseCapability成为? NewPromiseCapability( C ). NewPromiseCapability( C )。
  4. Perform ? 表演? Call( promiseCapability .[[Reject]], undefined, « r »). 调用( promiseCapability 。[[Reject]],undefined,«r»)。
  5. Return promiseCapability .[[Promise]]. 返回promiseCapability 。[[Promise]]。

NOTE: The reject function expects its this value to be a constructor function that supports the parameter conventions of the Promise constructor. 注意: reject函数期望它的这个值是一个构造函数,它支持Promise构造函数的参数约定。

As you can tell from this, Promise.reject expects to be called on a Promise constructor (either native promises or other compatible implementation). 从这里可以看出, Promise.reject期望在Promise构造函数(本机promise或其他兼容实现)上调用。 When treating Promise.reject as a first-class function like that, you're calling it on the global object, which is not a Promise constructor and therefore fails. 当将Promise.reject视为类似的第一类函数时,您将在全局对象上调用它,而该对象不是Promise构造函数,因此失败。 1 1

If you need to use Promise.reject in this way, I'd recommend to bind it first: 如果你需要以这种方式使用Promise.reject ,我建议先绑定它:

var f = Promise.reject.bind(Promise);
f(3); // Promise {[[PromiseStatus]]: "rejected", [[PromiseValue]]: 3}

1 I'm not exactly sure why the global object is not considered an object, though, since Promise.reject.call({ }) gives Uncaught TypeError: object is not a constructor . 1我不完全确定为什么全局对象不被视为对象,因为Promise.reject.call({ })给出了Uncaught TypeError: object is not a constructor

I believe that this is because reject is defined as a static function on the Promise object. 我相信这是因为reject被定义为Promise对象上的静态函数。

As such, you cannot call it as you have above, as it is called directly on the class and not on an instance of the class. 因此,您不能像上面那样调用它,因为它直接在类上调用,而不是在类的实例上调用。

More detail about static functions and when they can and cannot be called here: 有关静态函数的详细信息以及何时可以在此处调用它们:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/static https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/static

You could wrap it in a function: 你可以将它包装在一个函数中:

function promiseReject(x) {
   return Promise.reject(x);
}

var f = promiseReject;

var out = f(3);

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

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