Created
February 5, 2021 08:59
-
-
Save DmitryMyadzelets/7719681686bbed139d3d6e2621208b2f to your computer and use it in GitHub Desktop.
How to use async/await with map and Promise.all
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example from https://dev.to/jamesliudotcc/how-to-use-async-await-with-map-and-promise-all-1gb5 | |
// 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 | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment