`Promise.map` calls callback synchronously in bluebird v3.x
See original GitHub issue- 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?
Issue Analytics
- State:
- Created 7 years ago
- Reactions:2
- Comments:12 (11 by maintainers)
Top Results From Across the Web
Promise.map - Bluebird JS
The order map calls the mapper function on the array elements is not specified, there is no guarantee on the order in which...
Read more >Asynchronous exception handling with bluebird promises
Now i know promise does not catch error in async callback. Here is my 3 examples i have tested. Note: After call reject,...
Read more >Bluebird v2.9.27 API Reference
For example Promise.map(arr, fn) is the same as calling ... to use .then() to get at the promise's value as the callback is...
Read more >4. Using Libraries and Frameworks - JavaScript with Promises ...
js, and it is available as an npm package for use in Node.js. The Bluebird Promise object can serve as a drop-in replacement...
Read more >25. Promises for asynchronous programming - Exploring JS
Node.js: using callback-based sync functions with Promises; 25.14. ES6-compatible Promise libraries ... Composing asynchronous calls (loops, mapping, etc.): ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I am leaning towards patch because it’s a pretty “obvious” bug and not many people can be relying on the order being essentially random.
No, thank you! I’m pleased to be able to actually contribute fixes rather than just raising endless issues.