Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions src/ParsePromise.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,7 @@ export default class ParsePromise {
*/
static when(promises) {
var objects;
var arrayArgument = Array.isArray(promises);
if (arrayArgument) {
if (Array.isArray(promises)) {
objects = promises;
} else {
objects = arguments;
Expand All @@ -356,13 +355,12 @@ export default class ParsePromise {
var total = objects.length;
var hadError = false;
var results = [];
var returnValue = arrayArgument ? [results] : results;
var errors = [];
results.length = objects.length;
errors.length = objects.length;

if (total === 0) {
return ParsePromise.as.apply(this, returnValue);
return ParsePromise.as.apply(this, results);
}

var promise = new ParsePromise();
Expand All @@ -373,7 +371,7 @@ export default class ParsePromise {
if (hadError) {
promise.reject(errors);
} else {
promise.resolve.apply(promise, returnValue);
promise.resolve.apply(promise, results);
}
}
};
Expand Down
39 changes: 2 additions & 37 deletions src/__tests__/ParsePromise-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ describe('Promise', () => {
jest.runAllTimers();
}));

it('can handle promises in parallel with array', asyncHelper(function(done) {
it('can handle promises in parallel', asyncHelper(function(done) {
var COUNT = 5;

var delay = function(ms) {
Expand All @@ -249,42 +249,7 @@ describe('Promise', () => {
generate(i);
}

ParsePromise.when(promises).then(function(results) {
expect(called).toBe(COUNT);
expect(COUNT).toBe(results.length);
var actual = results;
for (var i = 0; i < actual.length; i++) {
expect(actual[i]).toBe(5 * i);
}
done();
});

jest.runAllTimers();
}));

it('can handle promises in parallel with arguments', asyncHelper(function(done) {
var COUNT = 5;

var delay = function(ms) {
var promise = new ParsePromise();
setTimeout(() => { promise.resolve(); }, ms);
return promise;
};

var called = 0;
var promises = [];
function generate(i) {
promises[i] = delay((i % 2) ? (i * 10) : (COUNT * 10) - (i * 10)).then(
function() {
called++;
return 5 * i;
});
}
for (var i = 0; i < COUNT; i++) {
generate(i);
}

ParsePromise.when.apply(null, promises).then(function() {
ParsePromise.when(promises).then(function() {
expect(called).toBe(COUNT);
expect(COUNT).toBe(arguments.length);
var actual = arguments;
Expand Down