5 ms·
I started to use async/await because react-native and now I can't live without it. In nodejs you can have it: npm install --save babel-core babel-loader babe
by alfonsodev 11y ago
I started to use async/await because react-native and now I can't live without it.
In nodejs you can have it:
npm install --save babel-core babel-loader babel-polyfill babel-preset-es2015 babel-preset-stage-0
Adding this to entry point file
require("babel-core/register");
require("babel-polyfill");
and having a .babelrc with these presets:
{
"presets": ["es2015", "stage-0"]
}
Then you can transpile with babel-cli
( sudo npm install -g babel-cli if you don't have it yet)
babel app.js -o transpiled_app.js
Make sure you have npm version 3, or it become too slow to initialise.
as @vdaniuk mention Chackra supports it, but did anyone tried this https://github.com/nodejs/node-chakracore https://github.com/nodejs/node-chakracore?
Is there any other way to get async/await today ?
- esailija 11y agocoroutines with generators are more powerful (cancellation, automatic resource cleanup when the generator terminates etc) and have worked for a long time out of the box. The only difference is that you write `yield` instead of `await` and `decorator(function*` instead of `async function`
- phpnode 11y agoBabel can transform async functions into Bluebird coroutines, so the best of both worlds - https://www.npmjs.com/package/babel-plugin-transform-async-to-module-method https://www.npmjs.com/package/babel-plugin-transform-async-t...
- esailija 11y agoYes but with this you lose the powerful features I just mentioned: automatic resource cleanup[1], cancellation, yield handlers.. don't you? 1. https://github.com/petkaantonov/bluebird/issues/1014 https://github.com/petkaantonov/bluebird/issues/1014
- phpnode 11y agoNo I think those should work with async functions as it is literally just translating `async` -> `yield`, but worth investigating.
- alfonsodev 11y agoThanks for commenting on that, I didn't think about those issues. I'll research on how much impact could have on performance. In the other hand I believe syntax is not the only difference. I'm not full time js dev, but after a quick search, I found Domenic pointing 4 differences here https://esdiscuss.org/topic/does-async-await-solve-a-real-problem https://esdiscuss.org/topic/does-async-await-solve-a-real-pr...
- esailija 11y agoThese seem to be mostly about undecorated generators. 3. Is a legit difference but it is actually a syntax error to do yield 1 + yield 2. So that is fixable in a backward compatible way.
- mtzaldo 11y agoCould you put a reference for the decorator/yield usage? Thanks
- esailija 11y agohttp://bluebirdjs.com/docs/api/promise.coroutine.html http://bluebirdjs.com/docs/api/promise.coroutine.html
- mtzaldo 11y agoThanks, I will review it.
- l1ambda 11y agohttps://github.com/llambda/koa-boiler/blob/master/app.js#L75 https://github.com/llambda/koa-boiler/blob/master/app.js#L75 is an example of Bluebird coroutines paired with Koa.
- davej 11y agoWorth considering the es2015-node5 preset instead. It will only transpile the features that are not already in node 5.x. The standard es2015 preset transpiles lots of APIs that are already natively available in node 5.