Extending Promises in ES6

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 03:47:06

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/29333540/extending-promises-in-es6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!