1 | const { buffer, flatten, pipeline, transform } = require('streaming-iterables')
|
2 | const got = require('got@11.8.1')
|
3 |
|
4 |
|
5 | const pokedex = async function* () {
|
6 | let offset = 0
|
7 | while(true) {
|
8 | const url = `https://pokeapi.co/api/v2/pokemon/?offset=${offset}`
|
9 | const { results: pokemon } = await got(url).json()
|
10 | if (pokemon.length === 0) {
|
11 | return
|
12 | }
|
13 | offset += pokemon.length
|
14 | yield pokemon
|
15 | }
|
16 | }
|
17 |
|
18 |
|
19 | const bufferTwo = buffer(2)
|
20 |
|
21 |
|
22 | const pokeLoader = transform(2, async ({ url }) => got(url).json())
|
23 |
|
24 |
|
25 | const pokePipe = pipeline(pokedex, bufferTwo, flatten, pokeLoader)
|
26 |
|
27 |
|
28 | const run = async () => {
|
29 | for await (const pokemon of pokePipe){
|
30 | console.log(pokemon.name)
|
31 | console.log(pokemon.sprites.front_default)
|
32 | }
|
33 | }
|
34 |
|
35 | run().then(() => console.log('caught them all!'))
|