Last active
July 9, 2019 15:38
-
-
Save crutchcorn/a5130b9d4b64ec36d48d18f85dec1953 to your computer and use it in GitHub Desktop.
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
const getPeeps = () => Promise.resolve([{name: 'Corbin', addr: 1}, {name: 'Nic', addr: 0}]) | |
const getAddr = (val) => Promise.resolve(val ? 'My home' : 'Their home') | |
// Pure promise solution | |
getPeeps() | |
.then(people => { | |
const peoplePromiseArr = people.map(person => { | |
return getAddr(person.addr).then(addr => { | |
person.addr = addr; | |
return person; | |
}) | |
}); | |
return Promise.all(peoplePromiseArr); | |
}) | |
.then(console.log) | |
// That but using async/await | |
(async () => { | |
const people = await getPeeps(); | |
const peoplePromiseArr = people.map(async person => { | |
const addr = await getAddr(person.addr); | |
person.addr = addr; | |
return person; | |
}) | |
const finalArr = await Promise.all(peoplePromiseArr); | |
console.log(finalArr); | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment