Skip to content

Instantly share code, notes, and snippets.

@vzvu3k6k
Last active March 19, 2016 15:33
Show Gist options
  • Save vzvu3k6k/ac4764b2d3c811f3970e to your computer and use it in GitHub Desktop.
Save vzvu3k6k/ac4764b2d3c811f3970e to your computer and use it in GitHub Desktop.
Use async generators via babel
*.out.js
### https://raw.github.com/github/gitignore/c696ec7b8ca5443513fe4ef0844c07129f6a2c6c/Node.gitignore
# Logs
logs
*.log
npm-debug.log*
# Runtime data
pids
*.pid
*.seed
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# node-waf configuration
.lock-wscript
# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules
jspm_packages
# Optional npm cache directory
.npm
# Optional REPL history
.node_repl_history
var babel = require('babel-core');
var fs = require('fs');
var code = fs.readFileSync(process.argv[2]);
var plugins = {
'transform-async-to-generator': [
'syntax-async-functions',
'syntax-async-generators',
'transform-async-to-generator'
],
'transform-async-to-module-method': [
'syntax-async-functions',
'syntax-async-generators',
['transform-async-to-module-method', {
// Without these options transforming fails.
module: 'bluebird',
method: 'coroutine'
}]
],
'transform-regenerator': [
'syntax-async-functions',
'syntax-async-generators',
['transform-regenerator', {
asyncGenerators: true,
generators: true,
async: true
}]
]
};
for (var key of Object.keys(plugins)) {
console.log(key);
var result = babel.transform(code, { plugins: plugins[key] });
var path = `out/${key}.out.js`;
fs.writeFileSync(path, result.code);
}
{
"dependencies": {
"babel-core": "^6.7.2",
"babel-plugin-syntax-async-functions": "^6.5.0",
"babel-plugin-syntax-async-generators": "^6.5.0",
"babel-plugin-transform-async-to-generator": "^6.7.0",
"babel-plugin-transform-async-to-module-method": "^6.7.0",
"babel-plugin-transform-regenerator": "^6.6.5"
},
"license": "CC0-1.0"
}
async function * asyncGenerator () {
await Promise.resolve(1);
yield Promise.resolve(1);
}
async function asyncFunction () {
await Promise.resolve(1);
}
function * generator () {
yield 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment