Last active
January 19, 2021 03:10
-
-
Save appsparkler/19dd27b9978eafc43945f23e8f02136b to your computer and use it in GitHub Desktop.
Getting more from promises.
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
// 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