Skip to content

Commit

Permalink
feat(website): update concur iterable examples
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerAberbach committed Nov 28, 2024
1 parent 2f49f78 commit 1d11bc3
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions website/docs/concepts/concurrent-iterable.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -189,26 +189,26 @@ We can iterate over concur iterables:
```js playground
import { asConcur } from 'lfi'

const concurIterable = asConcur([`sleep`, `climb`, `eat`])
const concurIterable = asConcur([`sloth`, `lazy`, `sleep`])

await concurIterable(console.log)
//=> sloth
//=> lazy
//=> sleep
//=> climb
//=> eat
```

We can manually filter and map them:

```js playground
import { asConcur } from 'lfi'

const concurIterable = asConcur([`sleep`, `climb`, `eat`])
const API_URL = `https://random-word-form.herokuapp.com/random/adjective`

const concurIterable = asConcur([`sloth`, `lazy`, `sleep`])

const transformedConcurIterable = apply =>
concurIterable(async verb => {
const [adjective] = await (
await fetch(`https://random-word-form.herokuapp.com/random/adjective`)
).json()
const [adjective] = await (await fetch(API_URL)).json()
if (adjective.length <= 5) {
// Too short!
return
Expand All @@ -218,21 +218,27 @@ const transformedConcurIterable = apply =>
})

await transformedConcurIterable(console.log)
// NOTE: This order may change between runs
//=> educated sloth
//=> favorite sleep
```

Or we can use `lfi`'s functions to filter and map them!

```js playground
import { asConcur, filterMapConcur, forEachConcur, pipe } from 'lfi'

const API_URL = `https://random-word-form.herokuapp.com/random/adjective`

await pipe(
asConcur([`sleep`, `climb`, `eat`]),
asConcur([`sloth`, `lazy`, `sleep`]),
filterMapConcur(async verb => {
const [adjective] = await (
await fetch(`https://random-word-form.herokuapp.com/random/adjective`)
).json()
const [adjective] = await (await fetch(API_URL)).json()
return adjective.length <= 5 ? null : `${adjective} ${verb}`
}),
forEachConcur(console.log),
)
// NOTE: This order may change between runs
//=> educated sloth
//=> favorite sleep
```

0 comments on commit 1d11bc3

Please sign in to comment.