Repository navigation
Expand file tree
/
Copy pathindex.md
More file actions
102 lines (80 loc) · 3.74 KB
/
Copy pathindex.md
File metadata and controls
102 lines (80 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
---
title: async function* expression
slug: Web/JavaScript/Reference/Operators/async_function*
page-type: javascript-operator
browser-compat: javascript.operators.async_generator_function
sidebar: jssidebar
---
The **`async function*`** keywords can be used to define an async generator function inside an expression.
You can also define async generator functions using the [`async function*` declaration](/en-US/docs/Web/JavaScript/Reference/Statements/async_function*).
{{InteractiveExample("JavaScript Demo: async function* expression", "taller")}}
```js interactive-example
async function joinAll(generator) {
let str = "";
for await (const val of generator()) {
str += val;
}
return str;
}
joinAll(async function* () {
yield await Promise.resolve("a");
yield await Promise.resolve("b");
yield await Promise.resolve("c");
}).then((str) => console.log(str));
// Expected output: "abc"
```
## Syntax
```js-nolint
async function* (param0) {
statements
}
async function* (param0, param1) {
statements
}
async function* (param0, param1, /* …, */ paramN) {
statements
}
async function* name(param0) {
statements
}
async function* name(param0, param1) {
statements
}
async function* name(param0, param1, /* …, */ paramN) {
statements
}
```
> [!NOTE]
> An [expression statement](/en-US/docs/Web/JavaScript/Reference/Statements/Expression_statement) cannot begin with the keywords `async function` to avoid ambiguity with an [`async function*` declaration](/en-US/docs/Web/JavaScript/Reference/Statements/async_function*). The `async function` keywords only begin an expression when they appear in a context that cannot accept statements.
### Parameters
- `name` {{optional_inline}}
- : The function name. Can be omitted, in which case the function is _anonymous_. The name is only local to the function body.
- `paramN` {{optional_inline}}
- : The name of a formal parameter for the function. For the parameters' syntax, see the [Functions reference](/en-US/docs/Web/JavaScript/Guide/Functions#function_parameters).
- `statements` {{optional_inline}}
- : The statements which comprise the body of the function.
## Description
An `async function*` expression is very similar to, and has almost the same syntax as, an [`async function*` declaration](/en-US/docs/Web/JavaScript/Reference/Statements/async_function*). The main difference between an `async function*` expression and an `async function*` declaration is the _function name_, which can be omitted in `async function*` expressions to create _anonymous_ functions. An `async function*` expression can be used as an [IIFE](/en-US/docs/Glossary/IIFE) (Immediately Invoked Function Expression) which runs as soon as it is defined, allowing you to create an ad-hoc [async iterable object](/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_async_iterator_and_async_iterable_protocols). See also the chapter about [functions](/en-US/docs/Web/JavaScript/Reference/Functions) for more information.
## Examples
### Using async function\* expression
The following example defines an unnamed asynchronous generator function and assigns it to `x`. The function yields the square of its argument:
```js
const x = async function* (y) {
yield Promise.resolve(y * y);
};
x(6)
.next()
.then((res) => console.log(res.value)); // 36
```
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}
## See also
- [Functions](/en-US/docs/Web/JavaScript/Guide/Functions) guide
- [Functions](/en-US/docs/Web/JavaScript/Reference/Functions)
- {{jsxref("Statements/async_function*", "async function*")}}
- {{jsxref("AsyncGeneratorFunction")}}
- [Iteration protocols](/en-US/docs/Web/JavaScript/Reference/Iteration_protocols)
- {{jsxref("Operators/yield", "yield")}}
- {{jsxref("Operators/yield*", "yield*")}}