-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Bluebird.coroutine() is sometimes synchronous #1170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Looks like a bug. |
Minimal test case: var Bluebird = require('bluebird');
var foo = Bluebird.coroutine(function* () {
yield Bluebird.resolve();
console.log(3);
})
console.log(1);
foo();
console.log(2); Basically, "yield" will synchronously inspect the promise and continue execution immediately if its already resolved. Since no handlers are added to the promise in this case, the async trampoline is completely avoided. |
Here's a very hacky workaround for until this gets fixed: var shimmer = require('shimmer');
var AltPromise = Promise.getNewLibraryCopy();
shimmer.wrap(Promise, 'coroutine', function(original) {
return function(generatorFunction, options) {
if (typeof generatorFunction === 'function') {
var generatorFunctionOriginal = generatorFunction;
generatorFunction = function() {
var generator = generatorFunctionOriginal.apply(this, arguments);
var next = generator.next;
generator.next = function() {
var ret = next.apply(this, arguments);
if (ret.value instanceof Promise && !ret.value.isPending()) ret.value = AltPromise.resolve(ret.value);
return ret;
};
return generator;
};
}
return original.call(this, generatorFunction, options);
};
}); (the problem only occurs when the yielded promise is an instance of the main bluebird Promise constructor) |
Amazing you guys fixed this so fast. Any idea when it's likely to get pushed to npm? |
This is with latest Bluebird (3.4.1) and Babel (6.11.4).
Many Babel users are using
Bluebird.coroutine()
in combination withtransform-async-to-module-method
to have Bluebird shimasync/await
.Unfortunately, it's not a perfect shim. The following code:
prints
1 3 2
withBluebird.coroutine()
and1 2 3
with the defaulttransform-async-to-generator
method.I've created this Phabricator ticket to track but it appears the issue lies here.
The above code transpiles to this, which I have cleaned up for readability:
This is easily runnable in any Node runtime that supports generators:
Is this intended behavior?
The text was updated successfully, but these errors were encountered: