blob: fef4bac06933bbc3d2654c5738e1de6c89c0b61e (
plain)
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
|
---
title: Symbol.asyncIterator
slug: Web/JavaScript/Reference/Global_Objects/Symbol/asyncIterator
tags:
- ECMAScript 2018
- JavaScript
- Propriété
- Reference
- Symbole
translation_of: Web/JavaScript/Reference/Global_Objects/Symbol/asyncIterator
original_slug: Web/JavaScript/Reference/Objets_globaux/Symbol/asyncIterator
---
{{JSRef}}
Le symbole connu **`Symbol.asyncIterator`** définit l'itérateur asynchrone par défaut d'un objet. Si cette propriété est définie sur un objet, celui-ci est un itérable asynchrone et peut être utilisé avec une boucle [`for await...of`](/fr/docs/Web/JavaScript/Reference/Instructions/for-await...of).
{{js_property_attributes(0,0,0)}}
## Description
Le symbole `Symbol.asyncIterator` est un symbole natif utilisé pour accéder à la méthode `@@asyncIterator` d'un objet. Pour qu'un objet soit un itérable asynchrone, il doit avoir une clé `Symbol.asyncIterator`.
## Exemples
### Itérable asynchrone personnalisé
Il est possible de définir son propre itérable en définissant la propriété `[Symbol.asyncIterator]` d'un objet :
```js
const myAsyncIterable = new Object();
myAsyncIterable[Symbol.asyncIterator] = async function*() {
yield "coucou";
yield "l'itération";
yield "asynchrone !";
};
(async () => {
for await (const x of myAsyncIterable) {
console.log(x);
// expected output:
// "coucou"
// "l'itération"
// "asynchrone !"
}
})();
```
### Itérables asynchrones natifs
Il n'existe actuellement pas d'objets JavaScript natifs qui possèdent la clé `[Symbol.asyncIterator]` par défaut. Toutefois, les flux (_Streams_) WHATWG pourraient devenir les premiers objets natifs itérables asynchrones.
## Spécifications
| Spécification | État | Commentaires |
| ---------------------------------------------------------------------------------------------------- | ------------------------ | ------------ |
| {{SpecName('ES2018', '#sec-symbol.asynciterator', 'Symbol.asyncIterator')}} | {{Spec2('ES2018')}} | |
## Compatibilité des navigateurs
{{compat("javascript.builtins.Symbol.asyncIterator")}}
## Voir aussi
- [Les protocoles d'itération](/fr/docs/Web/JavaScript/Reference/Les_protocoles_iteration)
- [`for await... of`](/fr/docs/Web/JavaScript/Reference/Instructions/for-await...of)
|