babel-plugin-transform-async-generator-functions
Advanced tools
Comparing version 6.17.0 to 6.22.0
{ | ||
"name": "babel-plugin-transform-async-generator-functions", | ||
"version": "6.17.0", | ||
"version": "6.22.0", | ||
"description": "Turn async generator functions into ES2015 generators", | ||
@@ -12,9 +12,9 @@ "repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-async-generator-functions", | ||
"dependencies": { | ||
"babel-helper-remap-async-to-generator": "^6.16.2", | ||
"babel-helper-remap-async-to-generator": "^6.22.0", | ||
"babel-plugin-syntax-async-generators": "^6.5.0", | ||
"babel-runtime": "^6.0.0" | ||
"babel-runtime": "^6.22.0" | ||
}, | ||
"devDependencies": { | ||
"babel-helper-plugin-test-runner": "^6.3.13" | ||
"babel-helper-plugin-test-runner": "^6.22.0" | ||
} | ||
} |
@@ -1,9 +0,77 @@ | ||
# babel-plugin-transform-async-functions | ||
# babel-plugin-transform-async-generator-functions | ||
Turn async generator functions and for-await statements to ES2015 generators | ||
> Turn async generator functions and for-await statements to ES2015 generators | ||
## Example | ||
**In** | ||
```javascript | ||
async function* agf() { | ||
await 1; | ||
yield 2; | ||
} | ||
``` | ||
**Out** | ||
```javascript | ||
var _asyncGenerator = ... | ||
let agf = (() => { | ||
var _ref = _asyncGenerator.wrap(function* () { | ||
yield _asyncGenerator.await(1); | ||
yield 2; | ||
}); | ||
return function agf() { | ||
return _ref.apply(this, arguments); | ||
}; | ||
})(); | ||
``` | ||
For await example | ||
```js | ||
async function f() { | ||
for await (let x of y) { | ||
g(x); | ||
} | ||
} | ||
``` | ||
**Example Usage** | ||
```js | ||
async function* genAnswers() { | ||
var stream = [ Promise.resolve(4), Promise.resolve(9), Promise.resolve(12) ]; | ||
var total = 0; | ||
for await (let val of stream) { | ||
total += await val; | ||
yield total; | ||
} | ||
} | ||
function forEach(ai, fn) { | ||
return ai.next().then(function (r) { | ||
if (!r.done) { | ||
fn(r); | ||
return forEach(ai, fn); | ||
} | ||
}); | ||
} | ||
var output = 0; | ||
forEach(genAnswers(), function(val) { output += val.value }) | ||
.then(function () { | ||
console.log(output); // 42 | ||
}); | ||
``` | ||
[Try it Out in the REPL](https://babeljs.io/repl/#?babili=false&evaluate=true&lineWrap=false&presets=stage-3&code=async%20function*%20genAnswers()%20%7B%0A%20%20var%20stream%20%3D%20%5B%20Promise.resolve(4)%2C%20Promise.resolve(9)%2C%20Promise.resolve(12)%20%5D%3B%0A%20%20var%20total%20%3D%200%3B%0A%20%20for%20await%20(let%20val%20of%20stream)%20%7B%0A%20%20%20%20total%20%2B%3D%20await%20val%3B%0A%20%20%20%20yield%20total%3B%0A%20%20%7D%0A%7D%0A%0Afunction%20forEach(ai%2C%20fn)%20%7B%0A%20%20return%20ai.next().then(function%20(r)%20%7B%0A%20%20%20%20if%20(!r.done)%20%7B%0A%20%20%20%20%20%20fn(r)%3B%0A%20%20%20%20%20%20return%20forEach(ai%2C%20fn)%3B%0A%20%20%20%20%7D%0A%20%20%7D)%3B%0A%7D%0A%0Avar%20output%20%3D%200%3B%0AforEach(genAnswers()%2C%20function(val)%20%7B%20output%20%2B%3D%20val.value%20%7D)%0A.then(function%20()%20%7B%0A%20%20console.log(output)%3B%20%2F%2F%2042%0A%7D)%3B&experimental=true&loose=false&spec=false&playground=true&stage=0) | ||
## Installation | ||
```sh | ||
$ npm install babel-plugin-transform-async-generator-functions | ||
npm install --save-dev babel-plugin-transform-async-generator-functions | ||
``` | ||
@@ -26,3 +94,3 @@ | ||
```sh | ||
$ babel --plugins transform-async-generator-functions script.js | ||
babel --plugins transform-async-generator-functions script.js | ||
``` | ||
@@ -37,1 +105,5 @@ | ||
``` | ||
## References | ||
* [Proposal: Asynchronous iteration for ECMAScript](https://github.com/tc39/proposal-async-iteration) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4724
108