Skip to content

Promise.map calls callback synchronously in bluebird v3.x #1148

@overlookmotel

Description

@overlookmotel
  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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions