question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`Promise.map` calls callback synchronously in bluebird v3.x

See original GitHub issue
  1. What version of bluebird is the issue happening on?

v3.0.0-3.4.1

  1. What platform and version? (For example Node.js 0.12 or Google Chrome 32)

Node v6.2.1, OS X 10.9.5

  1. 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

  1. Was this change in behavior intended? (If so, I feel it should be noted in the docs)
  2. If it wasn’t intended, would this be considered Zalgo?
  3. Is it inconsistent that Promise.mapSeries() doesn’t call the callback syncronously for the first round?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:12 (11 by maintainers)

github_iconTop GitHub Comments

2reactions
petkaantonovcommented, Aug 31, 2016

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.

0reactions
overlookmotelcommented, Sep 1, 2016

No, thank you! I’m pleased to be able to actually contribute fixes rather than just raising endless issues.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found