扩展ES6中的承诺 [英] Extending Promises in ES6

查看:93
本文介绍了扩展ES6中的承诺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  class PersistedPromise扩展Promise {} 

然后调用派生类上的静态 resolve 直接创建解决的承诺: p>

  PersistedPromise.resolve(1)

在traceur中,这将产生:

  ModuleEvaluationError:#< PersistedPromise>在新的PersistedPromise(〜rtm / gen / promise.js:6:57)不是一个承诺

在Function.resolve(本机)

在Babel(以$ code> babel-node --experimental promise.js 运行),它导致:

  Promise.apply(this,arguments); 
^
TypeError:[object Object]不是一个承诺
在新的PersistedPromise(〜rtm / gen / promise.js:1:23)
在Function.resolve )
...

我依赖于这一点:


Promise支持子类化的所有静态方法:他们通过接收方创建新的实例(认为:new this(...)),还可以通过它访问其他静态方法this.resolve(...)与Promise.resolve(...))。


from http://www.2ality.com/2014/10/es6-promises-api.html



似乎节点检查这个的调用,如 Promise.resolve.call(这个, val)作为承诺,而不是(正确的)承诺或其派生类(v0.12.0)。



上述不再有效,还是没有进入规范,或者只是不由traceur和/或node实现?

解决方案


上述不再有效,还是没有制定规范,或者只是由traceur和/或node实现?


ES6承诺在规范支持子类化。也就是说,你最终将能够按照刚才的方式对承诺进行子类化。这是设计。



那就是说,目前没有一个浏览器在这方面正确地遵循了这个规范 - 据我所知,只有ES6承诺的垫片,Babel(core-js)和RSVP遵循关于子类化正确的ES6语义。浏览器的支持终于来了,但还没有。紧紧抓住



这是一个目前支持的实现列表


I am trying to extend Promise:

class PersistedPromise extends Promise { }

Then call the static resolve on the derived class to directly create a resolved promise:

PersistedPromise.resolve(1)

In traceur, this yields:

ModuleEvaluationError: #<PersistedPromise> is not a promise
    at new PersistedPromise (~rtm/gen/promise.js:6:57)
    at Function.resolve (native)

In Babel (run as babel-node --experimental promise.js) it results in:

    Promise.apply(this, arguments);
            ^
TypeError: [object Object] is not a promise
    at new PersistedPromise (~rtm/gen/promise.js:1:23)
    at Function.resolve (native)
    ...

I was depending on this:

All static methods of Promise support subclassing: they create new instances via their receiver (think: new this(...)) and also access other static methods via it (this.resolve(...) versus Promise.resolve(...)).

from http://www.2ality.com/2014/10/es6-promises-api.html.

It appears that node checks the this on calls such as Promise.resolve.call(this, val) for being a Promise, rather than (correctly?) Promise or a derived class thereof (v0.12.0).

Is the above no longer operative, or did not make into the spec, or just not implemented by traceur and/or node?

解决方案

Is the above no longer operative, or did not make into the spec, or just not implemented by traceur and/or node?

ES6 promises in the spec support subclassing. That is, you will eventually be able to subclass promises the way you just did. This is by design.

That said, none of the browsers currently follow that spec correctly in this regard - as far as I know only the ES6-promise shim, Babel (core-js) and RSVP follow ES6 semantics with regards to subclassing correctly. Support in browsers is eventually coming but it's not there yet. Hold tight.

Here is a list of currently supporting implementations.

这篇关于扩展ES6中的承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆