leebyron / async-to-gen Goto Github PK
View Code? Open in Web Editor NEW⌛ Use async functions in your JavaScript today with speed and simplicity.
License: BSD 3-Clause "New" or "Revised" License
⌛ Use async functions in your JavaScript today with speed and simplicity.
License: BSD 3-Clause "New" or "Revised" License
So then we can just async-node -x .mjs,.js,.jsx -r @std/esm foo.mjs
When using require('async-to-gen/register');
Calling an async lambda as async () => {}
works
but calling async() => {}
will cause the new method to fail compilation.
Hello there,
Do someone know if it's possible to use async-to-gen with Jest ?
I can use it with ava or mocha with async-to-gen/register
but Jest don't seem to have a feature like that. On their Async docs they say to use babel-jest (https://facebook.github.io/jest/docs/tutorial-async.html#content).
Thanks
Test case --
let out;
async function longLoop() {
for (let i = 0; i < 1000000; i++) await undefined;
out = 1;
}
longLoop().then(() => console.log(1 === out));
Steps to reproduce --
Actual output --
➜ node --max-old-space-size=10 async-leak-gen.js
<--- Last few GCs --->
194 ms: Mark-sweep 16.0 (44.0) -> 15.9 (44.0) MB, 46.2 / 0.0 ms [allocation failure] [GC in old space requested].
242 ms: Mark-sweep 15.9 (44.0) -> 15.9 (44.0) MB, 48.6 / 0.0 ms [allocation failure] [GC in old space requested].
291 ms: Mark-sweep 15.9 (44.0) -> 15.7 (28.0) MB, 48.7 / 0.0 ms [last resort gc].
344 ms: Mark-sweep 15.7 (28.0) -> 15.7 (28.0) MB, 52.5 / 0.0 ms [last resort gc].
<--- JS stacktrace --->
==== JS stack trace =========================================
Security context: 0x170d0accfb51 <JS Object>
2: NewPromiseCapability(aka NewPromiseCapability) [native promise.js:175] [pc=0x379a595496a3] (this=0x170d0ac04381 <undefined>,O=0x170d0acc3059 <JS Function Promise (SharedFunctionInfo 0x170d0ac74f51)>)
3: then [native promise.js:~215] [pc=0x379a59554f01] (this=0x1c8d0c9c9839 <a Promise with map 0x3b1fbab1b9b9>,z=0x1c8d0c9ca109 <
JS Function s (SharedFunctionInfo 0x6e897d5d7c1)>,A=0x1c8d...
FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
1: node::Abort() [node]
2: 0x10a08dc [node]
3: v8::Utils::ReportApiFailure(char const*, char const*) [node]
4: v8::internal::V8::FatalProcessOutOfMemory(char const*, bool) [node]
5: v8::internal::Factory::NewOneByteInternalizedString(v8::internal::Vector<unsigned char const>, unsigned int) [node]
6: v8::internal::AstRawStringInternalizationKey::AsHandle(v8::internal::Isolate*) [node]
7: v8::internal::StringTable::LookupKey(v8::internal::Isolate*, v8::internal::HashTableKey*) [node]
8: v8::internal::AstValueFactory::GetString(unsigned int, bool, v8::internal::Vector<unsigned char const>) [node]
9: v8::internal::AstValueFactory::GetOneByteStringInternal(v8::internal::Vector<unsigned char const>) [node]
10: v8::internal::Scanner::CurrentSymbol(v8::internal::AstValueFactory*) [node]
11: v8::internal::ParserBase<v8::internal::ParserTraits>::ParseAndClassifyIdentifier(v8::internal::ExpressionClassifier<v8::internal
::ParserTraits>*, bool*) [node]
12: v8::internal::ParserBase<v8::internal::ParserTraits>::ParsePrimaryExpression(v8::internal::ExpressionClassifier<v8::internal::Pa
rserTraits>*, bool*) [node]
13: v8::internal::ParserBase<v8::internal::ParserTraits>::ParseMemberExpression(v8::internal::ExpressionClassifier<v8::internal::Par
serTraits>*, bool*) [node]
14: v8::internal::ParserBase<v8::internal::ParserTraits>::ParseMemberWithNewPrefixesExpression(v8::internal::ExpressionClassifier<v8
::internal::ParserTraits>*, bool*) [node]
15: v8::internal::ParserBase<v8::internal::ParserTraits>::ParseLeftHandSideExpression(v8::internal::ExpressionClassifier<v8::interna
l::ParserTraits>*, bool*) [node]
16: v8::internal::ParserBase<v8::internal::ParserTraits>::ParsePostfixExpression(v8::internal::ExpressionClassifier<v8::internal::Pa
... <snip>
Expected output --
Running non async-to-gen version with ToT v8
➜ ./out.gn/x64.release/d8 ~/code/scratch/async-leak.js --max-old-space-size 10
true
Reading register.js
, it looks like prev
is not called for matching files: https://github.com/leebyron/async-to-gen/blob/master/register.js#L9. I imagine this would prevent other register hooks, such as Babel's, from working in conjunction with async-to-gen
.
It isn't entirely clear to me how this should be handled. Perhaps simply raising an error if there is a preexisting require.extensions['.js']
and encouraging the user to load this before any other registers would work best.
Here is code I have:
class Dispatcher {
async dispatch(message) {
this.mailbox.enqueue(message);
if (!this.isDispatching) {
this.isDispatching = true;
for await (const message of this.mailbox) {
for (const task of this.tasks) task(message);
}
this.isDispatching = false;
}
}
}
After using async-to-gen Dispatcher.js > D.js
I'm seeing this:
class Dispatcher {
dispatch(message) {return __async(function*(){
this.mailbox.enqueue(message);
if (!this.isDispatching) {
this.isDispatching = true;
for (let $await1 of this.mailbox) {const message=yield{__await:$await1};
for (const task of this.queue) task(message);
}
this.isDispatching = false;
}
}.call(this))}
}
And there is a problem here:
const message=yield{__await:$await1};
A message
becomes an object with the actual value inside which breaks consequent logic.
Is it a bug or something wrong with my code? It works with babel-node
and babel-preset-stage-3
.
First off - thanks for a brilliant library. I'm using it in conjunction with FinishedPromise and mocha to write tests that cannot be slow :-)
I've found a bug though. This code:
async () => ({})
gets transpiled to this:
() =>__async(function*(){ (return {a:1}}()))
which is not valid JavaScript. I believe it ought to be translated to this (but I'm not 100% sure)
() =>__async(function*(){ return {a:1}}())
I tried various things in leaveArrowFunction
, but couldn't come up with something that worked without breaking the tests yet. Would be great to have a fix for this!
On mac it's UTF-8 .
Is it possible to set it to UTF-8 on windows too ?
This fails because of the wrapper function:
class Base {
async foo() { }
}
class Something extends Base {
async foo() {
return await super.foo();
}
}
Hi @leebyron, first of all thanks for such an awesome library! This + node6 = a dream.
I've got an issue where referencing arguments
in an async function is causing async-to-gen
to create an argument called arguments
, which is disallowed in strict mode. It causes the following to happen:
Here is the original bit of code at that point:
Is there some way to fix this? I was looking at the source and trying to understand why you need to explicitly pass arguments
through - how come you can't use apply
with the original args?
I got this error, don't know why yet (checking and will update when i know what caused it)
.../node_modules/async-to-gen/index.js:451
if (node && typeof node === 'object' && (node.type || node.length && node[0].type)) {
^
TypeError: Cannot read property 'type' of null
Probably a good idea to check if node[0].type
exists and throw if it doesn't 😬 with a possible cause (once i've found out what caused this) 💭
I have a small library written with async-to-gen
. At this point, I had to hack a bit like this:
// works
require('async-to-gen/register')({
excludes: /\/node_modules\/(?!my-library\/)/
})
On the other hand, async-to-gen
ignores node_modules
by default, so the code below doesn't work:
// doesn't work
require('async-to-gen/register')
Is there any other good ways to use async-to-gen
inside a library?
Or..., can we change this line if (isIncluded && !isExcluded)
to if (isIncluded || !isExcluded)
? Then we could simply write like this, I think:
// hope to work
require('async-to-gen/register')({
includes: /\/node_modules\/my-module\//
})
Related to #31
BTW, I'm enjoying async/await
. Thank you for your cool library 😄
I am using the zeit/micro script that requires async-to-gen, but now my runtime babel-register setup breaks. I filed a report there (#89), but I now believe it is because of this lib. Could you wrap your .js resolver AFTER the previously registered ones?
Wait, I have fixed it and will submit a pull request...
From the README:
require('async-to-gen/register')
require('./some-module-with-async-functions')
What if only need the hook for one file? Is there a way to remove the hook after it's done being useful? Something like:
var undo = require('async-to-gen/register')
require('./some-module-with-async-functions') // works
undo()
require('./another-module-with-async-functions') // breaks
Loving these standalone compiler modules :-D
Just created a tiny, tiny wrapper around this library for mocha: https://github.com/matthewmueller/async-mocha
May want to start something in the readme for including this compiler in some of the more common frameworks.
Nice work! ✨
This snippet works on Chrome 54 with native async/await
async function test() {
return !!await console.log('works');
}
test();
With async-to-gen
it leads to parse error. Simple workaround is to write return !!(await ....)
instead.
Hey Lee,
would you mind adding a license file to this repo?
Cheers.
I imagine many users may wish to use this with .jsx
files, and possibly also with .es6
or similar files.
Of course, they can always do this with a method other than the register hook. But it might be surprising that it won't work with jsx files.
It might also be worth documenting that in order for jsx to be processed, you would need to do something like this:
require('async-to-gen/register')
require('babel/register')
with async-to-gen
coming first. (see also #11)
Examples:
// iife
(async () => {
await test()
})()
// crockford-iife
(async () => {
await test()
}())
This fails to compile:
class Bar extends Object {
static async create(props) {
return 'foo';
}
};
Node error
/Users/ioksanen/git/mas/test.js:3
async create(props) {return __async(function*(){
^^^^^^
SyntaxError: Unexpected identifier
at Object.exports.runInThisContext (vm.js:76:16)
Problem seems to be static
keyword.
I think async-to-gen would be quite a bit more useful if it was more like async-node, where if you do async-to-gen index.js --out build
it'll walk the dependencies and write them out.
If that works for you, I think the easiest way would be to create a require.extensions
function that writes out the transformed files.
due to this line https://github.com/leebyron/async-to-gen/blob/master/register.js#L9 the require hook only works in development since if a package is installed from npm it will most likely contain node_modules
in its path.
is this on purpose?
if so, then maybe a line should be added to the readme alerting people to this.
I just wondered how this compares against https://github.com/MatAtBread/fast-async in terms of performance, etc?
I believe it is in everybody's best interest to join these efforts with the babel plugin. Their ecosystem is widely used and this module only makes problems for babel users.
If anybody wants to just run babel's plugin-async-to-generator, they can do so. If anybody has a problem with getting it to work, contact the authors about a documentation problem. Please don't go rogue on us and start making conflicting modules that don't play nice with require.extensions philosophy.
#11 and #12 are related to this.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.