loading...

re: How to use async/await with map and Promise.all VIEW POST

FULL DISCUSSION
 

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
)

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

 

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

 

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

 
code of conduct - report abuse