Commit f3880058 authored by Tristan Cavelier's avatar Tristan Cavelier

Promise.defer is synchronous now

parent fe46f81f
...@@ -358,13 +358,18 @@ Promise.prototype.defer = function (callback) { ...@@ -358,13 +358,18 @@ Promise.prototype.defer = function (callback) {
function createSolver() { function createSolver() {
return { return {
"resolve": function () { "resolve": function () {
var array;
if (that._state !== "resolved" && that._state !== "rejected") { if (that._state !== "resolved" && that._state !== "rejected") {
that._state = "resolved"; that._state = "resolved";
that._answers = arguments; that._answers = arguments;
that._onResolve.forEach(function (callback) { array = that._onResolve.slice();
setTimeout(function () { setTimeout(function () {
callback.apply(that, that._answers); var i;
}); for (i = 0; i < array.length; i += 1) {
try {
array[i].apply(that, that._answers);
} catch (ignore) {}
}
}); });
// free the memory // free the memory
that._onResolve = undefined; that._onResolve = undefined;
...@@ -373,13 +378,18 @@ Promise.prototype.defer = function (callback) { ...@@ -373,13 +378,18 @@ Promise.prototype.defer = function (callback) {
} }
}, },
"reject": function () { "reject": function () {
var array;
if (that._state !== "resolved" && that._state !== "rejected") { if (that._state !== "resolved" && that._state !== "rejected") {
that._state = "rejected"; that._state = "rejected";
that._answers = arguments; that._answers = arguments;
that._onReject.forEach(function (callback) { array = that._onReject.slice();
setTimeout(function () { setTimeout(function () {
callback.apply(that, that._answers); var i;
}); for (i = 0; i < array.length; i += 1) {
try {
array[i].apply(that, that._answers);
} catch (ignore) {}
}
}); });
// free the memory // free the memory
that._onResolve = undefined; that._onResolve = undefined;
...@@ -389,19 +399,19 @@ Promise.prototype.defer = function (callback) { ...@@ -389,19 +399,19 @@ Promise.prototype.defer = function (callback) {
}, },
"notify": function () { "notify": function () {
if (that._onProgress) { if (that._onProgress) {
var answers = arguments; var i;
that._onProgress.forEach(function (callback) { for (i = 0; i < that._onProgress.length; i += 1) {
callback.apply(that, answers); try {
}); that._onProgress[i].apply(that, arguments);
} catch (ignore) {}
}
} }
} }
}; };
} }
this._state = "running"; this._state = "running";
if (typeof callback === 'function') { if (typeof callback === 'function') {
setTimeout(function () { callback(createSolver());
callback(createSolver());
});
return this; return this;
} }
return createSolver(); return createSolver();
......
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