-
Notifications
You must be signed in to change notification settings - Fork 225
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
Can't Use 'await' in Fiber #402
Comments
This behavior is expected, but it's worth noting that a better system would have the following properties:
This is how fibers work with @laverdet For what it's worth, Meteor could abandon a lot of our custom transformation and |
The const Future = require('fibers/future');
let task = Future.task(function() {
let promise = Future.fromPromise(asyncStuff());
let future = fiberStuff();
Future.wait([ promise, future ]);
console.log(promise.get(), future.get());
});
(async function() {
await task.promise();
console.log('done with both');
})();
function asyncStuff() {
return new Promise(function(resolve, reject) {
setTimeout(() => resolve('done with async'), 2000);
});
}
function fiberStuff() {
let future = new Future;
setTimeout(() => future.return('done with fiber'), 2000);
return future;
} @benjamn it seems like such a modification would have surprising performance characteristics. Fibers are "lightweight" but not nearly as lightweight as the single-frame generators builtin to modern JS. The way promises and async functions are used tend to create several promises for a single RPC request. The basic usage example of |
I'm running in to similar issue where I have some code that is trying to use async / await, but after an await the code calls other code that is trying to use a Fiber (which it is no longer running in). Because in the stack the person uses an "async function(){}" we basically lose the ability for the rest of the code to run in a fiber. Bringing Davids code in to the thread:
Since async does some magic around Promise we have no way to instrument the function so that on return to the main call stack in continues executing in to a fiber. I think being able to mix Promises with Fibers is potentially very powerful as it allows people to use newer semantics where it makes sense and allows a complex codebase to not be entirely promise based. Any suggestions on how to make sure async functions return execution in to a fiber after an await? |
@benjamn I.E.:
Vs:
Correct? |
Any suggestions on how to work around using promise-based libraries in a Fiber? |
You can still use promises, just not await. The Fibers library has an example for converting promises to a fiber yield. It's pretty straight forward in that it is just passing the resume callback to the Promise then and catch functions |
If Meteor had only just now been created, they would have never messed with fibers, they would have just used async/await so that there's less API mismatch between server and client and none of this complexity of native v8 add-ons and promise wrapping. |
@jedwards1211 yeah duh :). I made fibers in 2011 because it was painfully obvious that the tools we had were no good. We didn't get generators in nodejs until September 2015 and async/await until February 2017. |
Yeah. What I'm actually annoyed about is that the author of Webdriverio kept a fiber-oriented approach in his most recent API breaking change, and it actually got more ungainly to use with |
index_js.txt
See attached example. After the 'await' call, fibers.current is not defined. This makes it impossible to use 'await' in a Fiber that also does asynchronous processing with callbacks.
The text was updated successfully, but these errors were encountered: