Skip to content

Instantly share code, notes, and snippets.

@appsparkler
Last active January 19, 2021 03:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save appsparkler/19dd27b9978eafc43945f23e8f02136b to your computer and use it in GitHub Desktop.
Save appsparkler/19dd27b9978eafc43945f23e8f02136b to your computer and use it in GitHub Desktop.
Getting more from promises.
// a sample array of items.
const values = [
'item1',
'item2'
]
// checkValue(item) is an async that returns true or false.
const promises = values.map(async(item) => checkValue(item).catch(err => err))
const results = await Promise.all(results)
console.log(results); // ['true', 'false'];
// what if we want the item too for ex: [{item: 'item1', result: true}, {item: 'item2', result: false}]
const promises2 = values.map(async(item)=> ({
item,
result: await checkValue(item).catch(err => err)
}))
const results2 = await Promise.all(promises2)
console.log(results2) // [{item: 'item1', result: true}, {item: 'item2', result: false}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment