-
Notifications
You must be signed in to change notification settings - Fork 29.7k
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
yield/await support in repl/cli #8382
Comments
@martinheidegger It already is if you run a generator function, I'm not 100 sure what you are asking. |
@Fishrock123 I think the idea is that the default repl wraps the code being run in a generator/async function so that the following works: > var x = await Promise.resolve(1); x
1 ~= async () => {
return eval(`var x = await Promise.resolve(1); x`);
}
|
@Fishrock123 @bmeck Explained my request better than me. |
My personal example is working with > var git = require('nodegit')
undefined
> var repo = await git.getRepo('.')
undefined
> await repo.getCommit(sha)
Commit { repo: Repository {} } |
@martinheidegger async/await functions were accepted in July : https://github.com/tc39/proposals/blob/master/finished-proposals.md |
+1 for such a feature. It would make it much easier to work with Promise-based APIs |
blocked on v8 giving us unflagged async/await functions at the very least. |
I wonder if a |
@Fishrock123 well modules have not fully landed top level await in spec (proposal in september?) so the module parser in v8 won't land the await outside of async functions for a while still. |
I mean for |
This is how I implemented in my electron based REPL. const awaitMatcher = /^(?:\s*(?:(?:let|var|const)\s)?\s*([^=]+)=\s*|^\s*)(await\s[\s\S]*)/;
const asyncWrapper = (code, binder) => {
let assign = binder ? `root.${binder} = ` : '';
return `(function(){ async function _wrap() { return ${assign}${code} } return _wrap();})()`;
};
// match & transform
const match = input.match(awaitMatcher);
if(match) {
input = `${asyncWrapper(match[2], match[1])}`;
} |
little workaround / hack:
or
or just of course the proposed addition of |
@princejwesley how can I add your implementation to node repl? I saw this as well: https://github.com/paulserraino/babel-repl but it does not support await syntax yet |
@martinheidegger what is the link for your personal working example of here: #8382 (comment) |
@sibelius Oh: I am afraid this is a misunderstanding: this is an example of how i would love it to work. |
@princejwesley it does work tks for the awesome work @martinheidegger u should try @princejwesley gist |
I was looking for similar solusion as well and I made this after I saw pannous's comment. > syncPromise(require('nodegit')).getRepo('.').o.getCommit(sha).o
Commit { repo: Repository {} } |
Removed |
@joyecheung It would be nice to reference the commit. |
@martinheidegger Sure, it should be PR #9618 and commit 2739185 |
@joyeecheung Awesome! Did you have a chance to test it? Does await work in the Repl? |
@martinheidegger, in v7.6.0 we have updated V8 to 5.5, which makes it possible to use async functions w/o the Apologize for the label shuffling. Didn't realize what it was actually blocking on. |
I made a small package https://www.npmjs.com/package/await-outside that you can use as
or just
Hope it helps anyone |
I just had a quick look at implementing this. Naively wrapping each input line with |
way too hacky if you insert multi line function etc |
you might notice that the tests of await-outside do cover multi-line inputs |
Hello, I wrote this quick and dirty Babel-based experiment: https://github.com/motet-a/async-repl It works as described by @Qard, so currently variable declarations are broken. But it’s not a big deal since we could transform variable declarations into global assigments only in the global scope with Babel, skipping declarations inside inner functions. It’s not perfect but it could be pretty reliable. |
in the mean time you could stub repl with https://github.com/ef4/async-repl |
BTW there is an open PR for |
Top-level await is now supported in the REPL. Closing. |
In the version of node 6.5.0 that I am running, the repl does support quite a few things already - which is awesome. One thing I came to miss when trying things out though is that the
yield
(as often used beforeawait
and/or theawait
keyword. Ifyield
would be available like withco
orawait
would be available like in the concept then async code would become easy to test in the REPL.The text was updated successfully, but these errors were encountered: