Skip to content

Commit

Permalink
ensure async calling of collection callbacks
Browse files Browse the repository at this point in the history
Fixes #513
  • Loading branch information
petkaantonov committed Feb 27, 2015
1 parent 8594d86 commit 0c571af
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/map.js
Expand Up @@ -5,6 +5,7 @@ module.exports = function(Promise,
tryConvertToPromise,
INTERNAL) {
var ASSERT = require("./assert.js");
var async = require("./async.js");
var util = require("./util.js");
var tryCatch = util.tryCatch;
var errorObj = util.errorObj;
Expand All @@ -21,9 +22,10 @@ function MappingPromiseArray(promises, fn, limit, _filter) {
this._limit = limit;
this._inFlight = 0;
this._queue = limit >= 1 ? [] : EMPTY_ARRAY;
this._init$(undefined, RESOLVE_ARRAY);
async.invoke(init, this, undefined);
}
util.inherits(MappingPromiseArray, PromiseArray);
function init() {this._init$(undefined, RESOLVE_ARRAY);}

// The following hack is required because the super constructor
// might call promiseFulfilled before this.callback = fn is set
Expand Down
6 changes: 5 additions & 1 deletion src/reduce.js
Expand Up @@ -5,6 +5,7 @@ module.exports = function(Promise,
tryConvertToPromise,
INTERNAL) {
var ASSERT = require("./assert.js");
var async = require("./async.js");
var util = require("./util.js");
var tryCatch = util.tryCatch;
var errorObj = util.errorObj;
Expand Down Expand Up @@ -39,7 +40,10 @@ function ReductionPromiseArray(promises, fn, accum, _each) {
if (!(isPromise || this._zerothIsAccum)) this._gotAccum = true;
this._callback = fn;
this._accum = accum;
if (!rejected) this._init$(undefined, RESOLVE_CALL_METHOD);
if (!rejected) async.invoke(init, this, undefined);
}
function init() {
this._init$(undefined, RESOLVE_CALL_METHOD);
}
util.inherits(ReductionPromiseArray, PromiseArray);

Expand Down
18 changes: 17 additions & 1 deletion test/mocha/regress.js
Expand Up @@ -97,5 +97,21 @@ describe("regressions", function() {
order.push(4);
assert.equal(order.join(","), "2,3,1,4");
});
})
});

specify("github-513", function() {
var order = [];
order.push(1);
var a = Promise.resolve([1]).each(function() {
order.push(4);
});
order.push(2);
var b = Promise.resolve([1]).map(function() {
order.push(5);
});
order.push(3);
return Promise.resolve().then(function() {
assert.deepEqual([1, 2, 3, 4, 5], order);
});
});
});

0 comments on commit 0c571af

Please sign in to comment.