Commit fe46f81f authored by Tristan Cavelier's avatar Tristan Cavelier

Promise.js perf increased

parent 4d9ba91a
...@@ -140,13 +140,13 @@ Promise.put = function (dict, property, value) { ...@@ -140,13 +140,13 @@ Promise.put = function (dict, property, value) {
* @return {Promise} The promise * @return {Promise} The promise
*/ */
Promise.execute = function (callback) { Promise.execute = function (callback) {
return new Promise().defer(function (solver) { var p = new Promise(), solver = p.defer();
try { try {
solver.resolve(callback()); Promise.when(callback(), solver.resolve, solver.reject);
} catch (e) { } catch (e) {
solver.reject(e); solver.reject(e);
} }
}); return p;
}; };
/** /**
...@@ -164,7 +164,7 @@ Promise.execute = function (callback) { ...@@ -164,7 +164,7 @@ Promise.execute = function (callback) {
* @return {Promise} The promise * @return {Promise} The promise
*/ */
Promise.all = function (items) { Promise.all = function (items) {
var array = [], count = 0, next = new Promise(), solver; var array = [], count = 0, next = new Promise(), solver, i;
solver = next.defer(); solver = next.defer();
function succeed(i) { function succeed(i) {
return function (answer) { return function (answer) {
...@@ -176,9 +176,9 @@ Promise.all = function (items) { ...@@ -176,9 +176,9 @@ Promise.all = function (items) {
return solver.resolve(array); return solver.resolve(array);
}; };
} }
items.forEach(function (item, i) { for (i = 0; i < items.length; i += 1) {
Promise.when(item).done(succeed(i)).fail(succeed(i)); Promise.when(items[i], succeed(i), succeed(i));
}); }
return next; return next;
}; };
...@@ -201,14 +201,14 @@ Promise.allOrNone = function (items) { ...@@ -201,14 +201,14 @@ Promise.allOrNone = function (items) {
var array = [], count = 0, next = new Promise(), solver; var array = [], count = 0, next = new Promise(), solver;
solver = next.defer(); solver = next.defer();
items.forEach(function (item, i) { items.forEach(function (item, i) {
Promise.when(item).done(function (answer) { Promise.when(item, function (answer) {
array[i] = answer; array[i] = answer;
count += 1; count += 1;
if (count !== items.length) { if (count !== items.length) {
return; return;
} }
return solver.resolve(array); return solver.resolve(array);
}).fail(function (answer) { }, function (answer) {
return solver.reject(answer); return solver.reject(answer);
}); });
}); });
...@@ -230,18 +230,17 @@ Promise.allOrNone = function (items) { ...@@ -230,18 +230,17 @@ Promise.allOrNone = function (items) {
* @return {Promise} The promise * @return {Promise} The promise
*/ */
Promise.any = function (items) { Promise.any = function (items) {
var count = 0, next = new Promise(), solver; var count = 0, next = new Promise(), solver, i;
solver = next.defer(); solver = next.defer();
items.forEach(function (item) { function onError(answer) {
Promise.when(item).done(function (answer) {
return solver.resolve(answer);
}).fail(function (answer) {
count += 1; count += 1;
if (count === items.length) { if (count === items.length) {
return solver.reject(answer); solver.reject(answer);
}
}
for (i = 0; i < items.length; i += 1) {
Promise.when(items[i], solver.resolve, onError);
} }
});
});
return next; return next;
}; };
...@@ -259,10 +258,10 @@ Promise.any = function (items) { ...@@ -259,10 +258,10 @@ Promise.any = function (items) {
* @return {Promise} The promise * @return {Promise} The promise
*/ */
Promise.first = function (items) { // *promises Promise.first = function (items) { // *promises
var next = new Promise(), solver = next.defer(); var next = new Promise(), solver = next.defer(), i;
items.forEach(function (item) { for (i = 0; i < items.length; i += 1) {
Promise.when(item).done(solver.resolve).fail(solver.reject); Promise.when(items[i], solver.resolve, solver.reject);
}); }
return next; return next;
}; };
...@@ -325,10 +324,10 @@ Promise.timeout = function (item, timeout) { ...@@ -325,10 +324,10 @@ Promise.timeout = function (item, timeout) {
i = setTimeout(function () { i = setTimeout(function () {
solver.reject.apply(next, [new Error("Timeout")]); solver.reject.apply(next, [new Error("Timeout")]);
}, timeout); }, timeout);
Promise.when(item).done(function () { Promise.when(item, function () {
clearTimeout(i); clearTimeout(i);
solver.resolve.apply(next, arguments); solver.resolve.apply(next, arguments);
}).fail(function () { }, function () {
clearTimeout(i); clearTimeout(i);
solver.reject.apply(next, arguments); solver.reject.apply(next, arguments);
}); });
......
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