Browse Source

+ Promise extends NativePromise

dev
RubaXa 10 years ago
parent
commit
aa4e21ed42
  1. 1
      src/Ply.es6
  2. 35
      src/promise.es6

1
src/Ply.es6

@ -738,6 +738,7 @@
Ply.each = _each; Ply.each = _each;
Ply.extend = _extend; Ply.extend = _extend;
Ply.promise = _promise; Ply.promise = _promise;
Ply.Promise = Promise;
Ply.support = support; Ply.support = support;
Ply.defaults = _defaults; Ply.defaults = _defaults;
Ply.attrName = _plyAttr; Ply.attrName = _plyAttr;

35
src/promise.es6

@ -2,8 +2,9 @@
* @desc «Обещания» * @desc «Обещания»
*/ */
var NativePromise = window.Promise,
var Promise = window.Deferred || window.Promise; Promise = window.Deferred || NativePromise
;
/** /**
@ -44,7 +45,7 @@ function _promiseAll(iterable) {
* @private * @private
*/ */
function _resolvePromise(value) { function _resolvePromise(value) {
return _promise((resolve) => resolve(value)); return _promise(resolve => resolve(value));
} }
@ -61,11 +62,35 @@ function _cast(value) {
// //
// Проверяем поддержку метода always // Проверяем поддержку методы: done, fail, always
// //
if (!_resolvePromise().always) { var __promise__ = _resolvePromise();
if (NativePromise && !__promise__.always) {
Promise = function (executor) {
var promise = new NativePromise(executor);
promise.__proto__ = this.__proto__;
return promise;
};
Promise.prototype = Object.create(NativePromise.prototype, { constructor: { value: Promise } });
Promise.prototype.done = function (callback) {
this.then(callback);
return this;
};
Promise.prototype.fail = function (callback) {
this['catch'](callback);
return this;
};
Promise.prototype.always = function (callback) { Promise.prototype.always = function (callback) {
this.then(callback, callback); this.then(callback, callback);
return this; return this;
}; };
['all', 'cast', 'reject', 'resolve'].forEach(name => {
Promise[name] = NativePromise[name];
});
} }

Loading…
Cancel
Save