DEV Community

Discussion on: How to use async/await with map and Promise.all

Collapse
 
kukuruznik profile image
kukuruznik • Edited

With some goodies it can look like:

// the goodies
const callMethod = (methodName, ...params) => obj => obj[methodName](...params)
const awaitAll = promiseArray => Promise.all(promiseArray)
const prop = propName => obj => obj[propName]
const map = func => arr => arr.map(func)
const pipe = (...functions) => functions.reduce((compound, func) => (input => func(compound(input))))
const download = url => fetch(url).then(callMethod("json"))

// the code
download(
    "http://swapi.co/api/people/2/"
)
.then(
    pipe(
        prop("films"),
        map(download),
        awaitAll,
    )
)
.then(
    console.log
)
.catch(
    console.error
)
Enter fullscreen mode Exit fullscreen mode

Even less cluttered than the async/await version, IMHO. You have prop, map and pipe in ramda, out of box.

Collapse
 
jamesliudotcc profile image
James Liu

This is great!

Collapse
 
gdebenito profile image
Gonzalo

I love what you've done here! 👏 so clean, so beautiful 🤩

Collapse
 
aizkhaj profile image
Aizaz Khaja

That's some next level abstractions with currying and clever use of array methods!