How to inherit native Promise?

/**
 * @class   MyPromise
 * @extends Promise
 */
var MyPromise = function (executor) {
  var promise = new Promise(executor);
  promise.__proto__ = this.__proto__;
  return promise;
};
 
MyPromise.prototype = Object.create(Promise.prototype, { constructor: { value: MyPromise } });
 
MyPromise.prototype.done = function (callback) {
  this.then(callback);
  return this;
};
 
MyPromise.prototype.fail = function (callback) {
  this['catch'](callback);
  return this;
};
 
MyPromise.prototype.always = function (callback) {
  this.then(callback, callback);
  return this;
};
 
MyPromise.all = Promise.all;
MyPromise.cast = Promise.cast;
MyPromise.reject = Promise.reject;
MyPromise.resolve = Promise.resolve;

http://code.re/5TC

2 комментария: