- What version of bluebird is the issue happening on?
v3.0.0-3.4.1
- What platform and version? (For example Node.js 0.12 or Google Chrome 32)
Node v6.2.1, OS X 10.9.5
- Did this issue happen with earlier version of bluebird?
No
Promise.map() calling callback sync/async
The behavior of Promise.map() seems to have changed between bluebird v2.10.2 and v3.0.0.
In v2.x, the callback is always called asynchronously; in v3.0.0-3.4.1 the callback is called synchronously for literal values of the array.
var arr = [ 1, 2, 3 ];
Promise.map( arr, function(v) {
console.log(v);
} );
console.log('next sync statement');
With bluebird v2.10.2:
next sync statement
1
2
3
With bluebird v3.x:
1
2
3
next sync statement
Resolved promises in the array are also mapped over synchronously. e.g. arr = [ 1, Promise.resolve(2), 3 ], gives the same result.
But any unresolved promises in the array are awaited before calling the callback on that item. This causes some puzzling behavior:
var arr = [ 1, Promise.resolve(2).tap( function() {} ), 3 ];
Promise.map( arr, function(v) {
console.log(v);
} );
console.log('next sync statement');
Outputs:
1
3
next sync statement
2
And also:
var iterator = (function*() {
yield 1;
yield Promise.resolve(2).tap(function() {});
yield 3;
})();
Promise.map( iterator, function(v) {
console.log(v);
} );
console.log('next sync statement');
1
3
next sync statement
2
This behavior is the same with Promise.filter() too.
Questions
- Was this change in behavior intended? (If so, I feel it should be noted in the docs)
- If it wasn't intended, would this be considered Zalgo?
- Is it inconsistent that
Promise.mapSeries() doesn't call the callback syncronously for the first round?
v3.0.0-3.4.1
Node v6.2.1, OS X 10.9.5
No
Promise.map()calling callback sync/asyncThe behavior of
Promise.map()seems to have changed between bluebird v2.10.2 and v3.0.0.In v2.x, the callback is always called asynchronously; in v3.0.0-3.4.1 the callback is called synchronously for literal values of the array.
With bluebird v2.10.2:
With bluebird v3.x:
Resolved promises in the array are also mapped over synchronously. e.g.
arr = [ 1, Promise.resolve(2), 3 ], gives the same result.But any unresolved promises in the array are awaited before calling the callback on that item. This causes some puzzling behavior:
Outputs:
And also:
This behavior is the same with
Promise.filter()too.Questions
Promise.mapSeries()doesn't call the callback syncronously for the first round?