Skip to content

Instantly share code, notes, and snippets.

@egdavid
Created March 2, 2022 19:40
Show Gist options
  • Save egdavid/ed05c390bc4e567b92c037ddc0e2d2b5 to your computer and use it in GitHub Desktop.
Save egdavid/ed05c390bc4e567b92c037ddc0e2d2b5 to your computer and use it in GitHub Desktop.
Clean Await promise.all()
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"))
download(
"http://swapi.co/api/people/2/"
)
.then(
pipe(
prop("films"),
map(download),
awaitAll,
)
)
.then(
console.log
)
.catch(
console.error
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment