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.join` calls callback synchronously

See original GitHub issue

Please answer the questions the best you can:

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

v2.10.2, v3.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?

Yes, consistent across v2.x and v3.x


Promise.join’s behavior in when it calls the callback (final argument) depends on the state of the promises it’s passed. If all promises are resolved, it runs the callback synchronously; but if any of the promises are pending, it runs callback async.

Promise.join(
    Promise.resolve(1),
    Promise.resolve(2),
    Promise.resolve(3),
    function(a, b, c) {
        console.log( {a: a, b: b, c: c} );
    }
);
console.log('next sync statement');

outputs:

{ a: 1, b: 2, c: 3 }
next sync statement

But:

Promise.join(
    Promise.resolve(1),
    Promise.resolve(2).tap( function() {} ),
    Promise.resolve(3),
    function(a, b, c) {
        console.log( {a: a, b: b, c: c} );
    }
);
console.log('next sync statement');
next sync statement
{ a: 1, b: 2, c: 3 }

I’m not completely sure of the intended behavior, but I tend to think that this unpredictability isn’t ideal and the callback should never be called synchronously.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Venryxcommented, Apr 11, 2017

Just thought I’d mention myself as such a person – and yes, it’s a kind-of weird use case. (though it’s only for debugging code, of course)

Anyway, for how to restore this synchronous behavior (in my case, for a stack-trace processing library with external source-maps), see here: https://github.com/stacktracejs/stacktrace.js/issues/188

0reactions
benjamingrcommented, Aug 31, 2016

I’m not sure if we should fix this in 2.0, if there is someone in the wild who is doing this (for whatever weird use case) it’d be a behavior change for them.

Read more comments on GitHub >

github_iconTop Results From Across the Web

how can we combine synchronous function, callback function ...
Promisify the callback, and send all three through Promise.all . function a() { return "a"; } function b(callback) { setTimeout(() ...
Read more >
Understanding the Event Loop, Callbacks, Promises, and ...
setTimeout takes two arguments: the function it will run asynchronously, and the amount of time it will wait before calling that function. In ......
Read more >
How to use promises - Learn web development | MDN
The function returns immediately and calls your callback when the operation is finished. With a promise-based API, the asynchronous function ...
Read more >
Synchronous vs Asynchronous JavaScript – Call Stack ...
It pulls a callback function from the queue to the call stack when the stack is empty. Now the callback function executes generally...
Read more >
25. Promises for asynchronous programming - Exploring JS
Chaining is simpler: If the callback of then() returns a Promise (e.g. the result of calling another Promise-based function) then then() returns that ......
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