Skip to content

Commit 6377516

Browse files
committed
Correção do asyncIterable
1 parent 751976b commit 6377516

File tree

1 file changed

+56
-45
lines changed

1 file changed

+56
-45
lines changed

src/js/asyncIterable.js

+56-45
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,71 @@
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' },
139
}
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))
1618
}
1719

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()
2321

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+
}
2537
}
2638

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)
3141
}
3242

33-
gerarFrase('Os melhores alimentos são ')
34-
3543

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,
4357
done: false
44-
});
45-
} else {
46-
return Promise.resolve({
47-
done: true
48-
});
58+
}
4959
}
5060
}
51-
};
52-
}
61+
}
62+
};
5363

54-
var iterator = asyncIteratorAlimentos();
5564

5665
(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

Comments
 (0)