-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add
async-do-expressions
docs (#2486)
- Loading branch information
Showing
4 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
--- | ||
id: babel-plugin-proposal-async-do-expressions | ||
title: @babel/plugin-proposal-async-do-expressions | ||
sidebar_label: async-do-expressions | ||
--- | ||
|
||
> The `async do { .. }` expression executes a block (with one or many statements in it) in an _asynchronous_ context, and the final statement completion value inside the block becomes the completion value of the _asynchronous_ code. | ||
## Example | ||
|
||
Issuing HTTP request in parallel | ||
|
||
```js | ||
Promise.all([ | ||
async do { | ||
const result = await fetch('https://example.com/A'); | ||
await result.json() | ||
}, | ||
async do { | ||
const result = await fetch('https://example.org/B'); | ||
await result.json() | ||
}, | ||
]).then(([a, b]) => { | ||
console.log("example.com/A", a); | ||
console.log("example.org/B", b); | ||
}) | ||
``` | ||
|
||
will be transformed to | ||
|
||
```js | ||
Promise.all([ | ||
(async () { | ||
const result = await fetch('https://example.com/A'); | ||
return await result.json() | ||
})(), | ||
(async () { | ||
const result = await fetch('https://example.org/B'); | ||
return await result.json() | ||
})(), | ||
]).then(([a, b]) => { | ||
console.log("example.com/A", a); | ||
console.log("example.org/B", b); | ||
}) | ||
``` | ||
|
||
## Installation | ||
|
||
```sh | ||
npm install --save-dev @babel/plugin-proposal-async-do-expressions | ||
``` | ||
|
||
## Usage | ||
|
||
### With a configuration file (Recommended) | ||
|
||
```json | ||
{ | ||
"plugins": ["@babel/plugin-proposal-async-do-expressions"] | ||
} | ||
``` | ||
|
||
Note: This plugin transpiles `async do {}` to ES2017 Async arrow function `async () => {}`. If you target to an older engine, i.e. Node.js 6 or IE 11, please also add [`@babel/plugin-transform-async-to-generator`](plugin-transform-async-to-generator.md): | ||
|
||
``` | ||
{ | ||
"plugins": [ | ||
"@babel/plugin-proposal-async-do-expressions", | ||
"@babel/plugin-transform-async-to-generator" | ||
] | ||
} | ||
``` | ||
|
||
### Via CLI | ||
|
||
```sh | ||
babel --plugins @babel/plugin-proposal-async-do-expressions script.js | ||
``` | ||
|
||
### Via Node API | ||
|
||
```javascript | ||
require("@babel/core").transformSync("code", { | ||
plugins: ["@babel/plugin-proposal-async-do-expressions"], | ||
}); | ||
``` | ||
|
||
## References | ||
|
||
- [Proposal](https://github.com/tc39/proposal-async-do-expressions) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
--- | ||
id: babel-plugin-syntax-async-do-expressions | ||
title: @babel/plugin-syntax-async-do-expressions | ||
sidebar_label: syntax-async-do-expressions | ||
--- | ||
|
||
> #### Syntax only | ||
> | ||
> It's unlikely you want to use this plugin directly as it only enables Babel to parse this syntax. Instead, use [plugin-proposal-async-do-expressions](plugin-proposal-async-do-expressions.md) to _both_ parse and transform this syntax. | ||
## Installation | ||
|
||
```sh | ||
npm install --save-dev @babel/plugin-syntax-async-do-expressions | ||
``` | ||
|
||
## Usage | ||
|
||
### With a configuration file (Recommended) | ||
|
||
```json | ||
{ | ||
"plugins": ["@babel/plugin-syntax-async-do-expressions"] | ||
} | ||
``` | ||
|
||
### Via CLI | ||
|
||
```sh | ||
babel --plugins @babel/plugin-syntax-async-do-expressions script.js | ||
``` | ||
|
||
### Via Node API | ||
|
||
```javascript | ||
require("@babel/core").transformSync("code", { | ||
plugins: ["@babel/plugin-syntax-async-do-expressions"], | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters