1
- function AsyncIterable ( ...args ) {
2
- let index = 0
3
- let asyncIterable = {
4
- [ Symbol . asyncIterator ] ( ) {
5
- return this ;
6
- } ,
7
- next ( ) {
8
- if ( index < args . length ) {
9
- return Promise . resolve ( { value : args [ index ++ ] , done : false } )
10
- } else {
11
- return Promise . resolve ( { done : true } )
12
- }
1
+ //iterable com async await
2
+ function tabelaAlimentos ( ) {
3
+ const tabelas = {
4
+ alimento : {
5
+ 1 : { nome : 'Maça' } ,
6
+ 2 : { nome : 'Pizza' } ,
7
+ 3 : { nome : 'Feijoada' } ,
8
+ 4 : { nome : 'Hamburguer' } ,
13
9
}
14
- } ;
15
- return asyncIterable ;
10
+ }
11
+ return {
12
+ getSync : ( nome , id ) => tabelas [ nome ] [ id ] ,
13
+ getAsync : ( nome , id ) => delay ( 50 ) . then ( ( ) => tabelas [ nome ] [ id ] ) ,
14
+ }
15
+ }
16
+ function delay ( ms ) {
17
+ return new Promise ( resolve => setTimeout ( resolve , ms ) )
16
18
}
17
19
18
- async function iteracaoSobreAsyncIterable ( text ) {
19
- let newText = text ;
20
- for await ( let val of AsyncIterable ( 'hamburguer' , 'pizza' , 'danoninho' ) ) {
21
- newText += `${ val } , ` ;
22
- }
20
+ const tabela = tabelaAlimentos ( )
23
21
24
- return newText ;
22
+ //iterable normal
23
+ const asyncIteratorAlimentos = {
24
+ [ Symbol . iterator ] : ( ) => {
25
+ let i = 0 ;
26
+ return {
27
+ next : ( ) => {
28
+ i ++ ;
29
+ const alimento = tabela . getSync ( 'alimento' , i )
30
+ if ( alimento ) {
31
+ return { value : alimento , done : false } ;
32
+ }
33
+ return { done : true }
34
+ }
35
+ } ;
36
+ }
25
37
}
26
38
27
- async function gerarFrase ( text ) {
28
- let listaAlimento = await iteracaoSobreAsyncIterable ( text ) ;
29
- let alimentosHtml = document . getElementById ( 'alimentos' ) ;
30
- alimentosHtml . innerHTML = '<p>' + listaAlimento + '</p>' ;
39
+ for ( const alimento of asyncIteratorAlimentos ) {
40
+ console . log ( alimento )
31
41
}
32
42
33
- gerarFrase ( 'Os melhores alimentos são ' )
34
-
35
43
36
- function asyncIteratorAlimentos ( ) {
37
- const listaAlimentos = [ { alimento : 'pizza' , caloria : 2000 } , { alimento : 'Hamburguer' , caloria : 1000 } , { alimento : 'danoninho' , caloria : 5000 } ] ;
38
- return {
39
- next : function ( ) {
40
- if ( listaAlimentos . length ) {
41
- return Promise . resolve ( {
42
- value : listaAlimentos . shift ( ) ,
44
+ //iterable async
45
+ const alimentosAsync = {
46
+ [ Symbol . iterator ] : function ( ) {
47
+ let i = 0 ;
48
+ return {
49
+ next : async function ( ) {
50
+ i ++ ;
51
+ const alimento = await tabela . getAsync ( 'alimento' , i )
52
+ if ( ! alimento ) {
53
+ return { done : true }
54
+ }
55
+ return {
56
+ value : alimento ,
43
57
done : false
44
- } ) ;
45
- } else {
46
- return Promise . resolve ( {
47
- done : true
48
- } ) ;
58
+ }
49
59
}
50
60
}
51
- } ;
52
- }
61
+ }
62
+ } ;
53
63
54
- var iterator = asyncIteratorAlimentos ( ) ;
55
64
56
65
( async function ( ) {
57
- await iterator . next ( ) . then ( console . log ) ;
58
- await iterator . next ( ) . then ( console . log ) ;
59
- await iterator . next ( ) . then ( console . log ) ;
60
- } ) ( ) ;
66
+ const iterator = alimentosAsync [ Symbol . iterator ] ( )
67
+ console . log ( ( await iterator . next ( ) ) . value )
68
+ console . log ( ( await iterator . next ( ) ) . value )
69
+ console . log ( ( await iterator . next ( ) ) . value )
70
+ console . log ( ( await iterator . next ( ) ) . value )
71
+ } ) ( )
0 commit comments