Skip to content
Advertisement

Stop awaiting remaining promises if one returns sentinel value

I have a function validateTables() that validates that data exists in several tables using a call (per table) to an async helper function queryTable() that queries the api. To pass validation, data must exist in every table. If a table is empty, the helper function will return false. I currently have the set of calls in a Promise.all() that checks the resulting array for any false values. For performance, I would rather stop awaiting the resolution of any remaining promises if and when a promise resolves to false. Promise.race() and .all() don’t work because they are concerned with when or if a promise resolves, not the returned value. Can I do this without losing the parallel processing of the async functions?

Generalized functions:

JavaScript

Advertisement

Answer

The promise returned by Promise.all will reject as soon as any of the containing promises reject. With that in mind, you could throw the sentinel value instead of returning it, and simply check for that in a try/catch around the await.

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement