Commit bfef5658 authored by Tristan Cavelier's avatar Tristan Cavelier

Promise.js then chain breaks -> fixed

parent f3880058
...@@ -440,53 +440,79 @@ Promise.prototype.defer = function (callback) { ...@@ -440,53 +440,79 @@ Promise.prototype.defer = function (callback) {
* @return {Promise} The new promise * @return {Promise} The new promise
*/ */
Promise.prototype.then = function (onSuccess, onError, onProgress) { Promise.prototype.then = function (onSuccess, onError, onProgress) {
var next = new Promise(), that = this; var next = new Promise(), that = this, resolver = next.defer();
switch (this._state) { switch (this._state) {
case "resolved": case "resolved":
if (typeof onSuccess === 'function') { if (typeof onSuccess === 'function') {
next.defer(function (resolver) { setTimeout(function () {
try { try {
resolver.resolve(onSuccess.apply(that, that._answers)); Promise.when(
onSuccess.apply(that, that._answers),
resolver.resolve,
resolver.reject
);
} catch (e) { } catch (e) {
resolver.reject(e); resolver.reject(e);
} }
}); });
} else {
setTimeout(function () {
resolver.resolve();
});
} }
break; break;
case "rejected": case "rejected":
if (typeof onError === 'function') { if (typeof onError === 'function') {
next.defer(function (resolver) { setTimeout(function () {
try { try {
resolver.resolve(onError.apply(that, that._answers)); Promise.when(
onError.apply(that, that._answers),
resolver.reject,
resolver.reject
);
} catch (e) { } catch (e) {
resolver.reject(e); resolver.reject(e);
} }
}); });
} else {
setTimeout(function () {
resolver.reject.apply(resolver, that._answers);
});
} }
break; break;
default: default:
if (typeof onSuccess === 'function') { if (typeof onSuccess === 'function') {
this._onResolve.push(function () { this._onResolve.push(function () {
var answers = arguments; try {
next.defer(function (resolver) { Promise.when(
try { onSuccess.apply(that, arguments),
resolver.resolve(onSuccess.apply(that, answers)); resolver.resolve,
} catch (e) { resolver.reject
resolver.reject(e); );
} } catch (e) {
}); resolver.reject(e);
}
});
} else {
this._onResolve.push(function () {
resolver.resolve();
}); });
} }
if (typeof onError === 'function') { if (typeof onError === 'function') {
this._onReject.push(function () { this._onReject.push(function () {
var answers = arguments; try {
next.defer(function (resolver) { Promise.when(
try { onError.apply(that, that._answers),
resolver.resolve(onError.apply(that, answers)); resolver.reject,
} catch (e) { resolver.reject
resolver.reject(e); );
} } catch (e) {
}); resolver.reject(e);
}
});
} else {
this._onReject.push(function () {
resolver.reject.apply(resolver, that._answers);
}); });
} }
if (typeof onProgress === 'function') { if (typeof onProgress === 'function') {
...@@ -550,7 +576,9 @@ Promise.prototype.done = function (callback) { ...@@ -550,7 +576,9 @@ Promise.prototype.done = function (callback) {
switch (this._state) { switch (this._state) {
case "resolved": case "resolved":
setTimeout(function () { setTimeout(function () {
callback.apply(that, that._answers); try {
callback.apply(that, that._answers);
} catch (ignore) {}
}); });
break; break;
case "rejected": case "rejected":
...@@ -583,7 +611,9 @@ Promise.prototype.fail = function (callback) { ...@@ -583,7 +611,9 @@ Promise.prototype.fail = function (callback) {
switch (this._state) { switch (this._state) {
case "rejected": case "rejected":
setTimeout(function () { setTimeout(function () {
callback.apply(that, that._answers); try {
callback.apply(that, that._answers);
} catch (ignore) {}
}); });
break; break;
case "resolved": case "resolved":
...@@ -646,7 +676,9 @@ Promise.prototype.always = function (callback) { ...@@ -646,7 +676,9 @@ Promise.prototype.always = function (callback) {
case "resolved": case "resolved":
case "rejected": case "rejected":
setTimeout(function () { setTimeout(function () {
callback.apply(that, that._answers); try {
callback.apply(that, that._answers);
} catch (ignore) {}
}); });
break; break;
default: default:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment