Commit 71f121c1 authored by Stefan Penner's avatar Stefan Penner

cleanup RSVP.all

parent 8c11dcc8
...@@ -39,7 +39,8 @@ define("rsvp/all", ...@@ -39,7 +39,8 @@ define("rsvp/all",
function all(promises) { function all(promises) {
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
var results = [], remaining = promises.length; var results = [], remaining = promises.length,
promise;
if (remaining === 0) { if (remaining === 0) {
resolve([]); resolve([]);
...@@ -59,10 +60,12 @@ define("rsvp/all", ...@@ -59,10 +60,12 @@ define("rsvp/all",
} }
for (var i = 0; i < promises.length; i++) { for (var i = 0; i < promises.length; i++) {
if (promises[i] && typeof promises[i].then === 'function') { promise = promises[i];
promises[i].then(resolver(i), reject);
if (promise && typeof promise.then === 'function') {
promise.then(resolver(i), reject);
} else { } else {
resolveAll(i, promises[i]); resolveAll(i, promise);
} }
} }
}); });
...@@ -315,50 +318,7 @@ define("rsvp/hash", ...@@ -315,50 +318,7 @@ define("rsvp/hash",
__exports__.hash = hash; __exports__.hash = hash;
}); });
define("rsvp/node",
["rsvp/promise","rsvp/all","exports"],
function(__dependency1__, __dependency2__, __exports__) {
"use strict";
var Promise = __dependency1__.Promise;
var all = __dependency2__.all;
function makeNodeCallbackFor(resolve, reject) {
return function (error, value) {
if (error) {
reject(error);
} else if (arguments.length > 2) {
resolve(Array.prototype.slice.call(arguments, 1));
} else {
resolve(value);
}
};
}
function denodeify(nodeFunc) {
return function() {
var nodeArgs = Array.prototype.slice.call(arguments), resolve, reject;
var promise = new Promise(function(nodeResolve, nodeReject) {
resolve = nodeResolve;
reject = nodeReject;
});
all(nodeArgs).then(function(nodeArgs) {
nodeArgs.push(makeNodeCallbackFor(resolve, reject));
try {
nodeFunc.apply(this, nodeArgs);
} catch(e) {
reject(e);
}
});
return promise;
};
}
__exports__.denodeify = denodeify;
});
define("rsvp/promise", define("rsvp/promise",
["rsvp/config","rsvp/events","exports"], ["rsvp/config","rsvp/events","exports"],
...@@ -568,12 +528,12 @@ define("rsvp/resolve", ...@@ -568,12 +528,12 @@ define("rsvp/resolve",
return typeof x === "function" || (typeof x === "object" && x !== null); return typeof x === "function" || (typeof x === "object" && x !== null);
} }
function resolve(thenable){ function resolve(thenable) {
if (thenable instanceof Promise) { if (thenable instanceof Promise) {
return thenable; return thenable;
} }
var promise = new Promise(function(resolve, reject){ var promise = new Promise(function(resolve, reject) {
var then; var then;
try { try {
......
This diff is collapsed.
...@@ -6,7 +6,8 @@ define( ...@@ -6,7 +6,8 @@ define(
function all(promises) { function all(promises) {
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
var results = [], remaining = promises.length; var results = [], remaining = promises.length,
promise;
if (remaining === 0) { if (remaining === 0) {
resolve([]); resolve([]);
...@@ -26,10 +27,12 @@ define( ...@@ -26,10 +27,12 @@ define(
} }
for (var i = 0; i < promises.length; i++) { for (var i = 0; i < promises.length; i++) {
if (promises[i] && typeof promises[i].then === 'function') { promise = promises[i];
promises[i].then(resolver(i), reject);
if (promise && typeof promise.then === 'function') {
promise.then(resolver(i), reject);
} else { } else {
resolveAll(i, promises[i]); resolveAll(i, promise);
} }
} }
}); });
......
define(
["rsvp/promise","rsvp/all","exports"],
function(__dependency1__, __dependency2__, __exports__) {
"use strict";
var Promise = __dependency1__.Promise;
var all = __dependency2__.all;
function makeNodeCallbackFor(resolve, reject) {
return function (error, value) {
if (error) {
reject(error);
} else if (arguments.length > 2) {
resolve(Array.prototype.slice.call(arguments, 1));
} else {
resolve(value);
}
};
}
function denodeify(nodeFunc) {
return function() {
var nodeArgs = Array.prototype.slice.call(arguments), resolve, reject;
var promise = new Promise(function(nodeResolve, nodeReject) {
resolve = nodeResolve;
reject = nodeReject;
});
all(nodeArgs).then(function(nodeArgs) {
nodeArgs.push(makeNodeCallbackFor(resolve, reject));
try {
nodeFunc.apply(this, nodeArgs);
} catch(e) {
reject(e);
}
});
return promise;
};
}
__exports__.denodeify = denodeify;
});
...@@ -8,12 +8,12 @@ define( ...@@ -8,12 +8,12 @@ define(
return typeof x === "function" || (typeof x === "object" && x !== null); return typeof x === "function" || (typeof x === "object" && x !== null);
} }
function resolve(thenable){ function resolve(thenable) {
if (thenable instanceof Promise) { if (thenable instanceof Promise) {
return thenable; return thenable;
} }
var promise = new Promise(function(resolve, reject){ var promise = new Promise(function(resolve, reject) {
var then; var then;
try { try {
......
...@@ -2,7 +2,8 @@ import { Promise } from "rsvp/promise"; ...@@ -2,7 +2,8 @@ import { Promise } from "rsvp/promise";
function all(promises) { function all(promises) {
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
var results = [], remaining = promises.length; var results = [], remaining = promises.length,
promise;
if (remaining === 0) { if (remaining === 0) {
resolve([]); resolve([]);
...@@ -22,10 +23,12 @@ function all(promises) { ...@@ -22,10 +23,12 @@ function all(promises) {
} }
for (var i = 0; i < promises.length; i++) { for (var i = 0; i < promises.length; i++) {
if (promises[i] && typeof promises[i].then === 'function') { promise = promises[i];
promises[i].then(resolver(i), reject);
if (promise && typeof promise.then === 'function') {
promise.then(resolver(i), reject);
} else { } else {
resolveAll(i, promises[i]); resolveAll(i, promise);
} }
} }
}); });
......
...@@ -4,12 +4,12 @@ function objectOrFunction(x) { ...@@ -4,12 +4,12 @@ function objectOrFunction(x) {
return typeof x === "function" || (typeof x === "object" && x !== null); return typeof x === "function" || (typeof x === "object" && x !== null);
} }
function resolve(thenable){ function resolve(thenable) {
if (thenable instanceof Promise) { if (thenable instanceof Promise) {
return thenable; return thenable;
} }
var promise = new Promise(function(resolve, reject){ var promise = new Promise(function(resolve, reject) {
var then; var then;
try { try {
......
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