-
Notifications
You must be signed in to change notification settings - Fork 22.4k
/
index.md
80 lines (58 loc) · 2.42 KB
/
index.md
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
---
title: AsyncGenerator.prototype.throw()
slug: Web/JavaScript/Reference/Global_Objects/AsyncGenerator/throw
page-type: javascript-instance-method
browser-compat: javascript.builtins.AsyncGenerator.throw
---
{{JSRef}}
The **`throw()`** method of {{jsxref("AsyncGenerator")}} instances acts as if a `throw` statement is inserted in the generator's body at the current suspended position, which informs the generator of an error condition and allows it to handle the error, or perform cleanup and close itself.
## Syntax
```js-nolint
asyncGeneratorInstance.throw(exception)
```
### Parameters
- `exception`
- : The exception to throw. For debugging purposes, it is useful to make it an `instanceof` {{jsxref("Error")}}.
### Return value
If the thrown error is not caught, it will return a {{jsxref("Promise")}} which rejects with the exception passed in.
If the exception is caught by a [`try...catch`](/en-US/docs/Web/JavaScript/Reference/Statements/try...catch) and the generator resumes to yield more values, it will return a {{jsxref("Promise")}} which resolves with an {{jsxref("Object")}} with two properties:
- `done`
- : A boolean value:
- `true` if the generator function's control flow has reached the end.
- `false` if the generator function is able to produce more values.
- `value`
- : The value yielded from the next `yield` expression.
## Examples
### Using throw()
The following example shows a simple generator and an error that is thrown using the `throw` method. An error can be caught by a {{jsxref("Statements/try...catch", "try...catch")}} block as usual.
```js
// An async task. Pretend it's doing something more useful
// in practice.
function sleep(time) {
return new Promise((resolve, reject) => {
setTimeout(resolve, time);
});
}
async function* createAsyncGenerator() {
while (true) {
try {
await sleep(500);
yield 42;
} catch (e) {
console.error(e);
}
}
}
const asyncGen = createAsyncGenerator();
asyncGen.next(1).then((res) => console.log(res)); // { value: 42, done: false }
asyncGen
.throw(new Error("Something went wrong")) // Error: Something went wrong
.then((res) => console.log(res)); // { value: 42, done: false }
```
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}
## See also
- {{jsxref("Statements/async_function*", "async function*")}}
- [Iterators and generators](/en-US/docs/Web/JavaScript/Guide/Iterators_and_generators) guide