Skip to content

Instantly share code, notes, and snippets.

@jodyheavener
Created July 3, 2020 03:23
Show Gist options
  • Select an option

  • Save jodyheavener/365c70a7cc2da7465a394e3ba60fd30a to your computer and use it in GitHub Desktop.

Select an option

Save jodyheavener/365c70a7cc2da7465a394e3ba60fd30a to your computer and use it in GitHub Desktop.
short-circuit attempts thwarted by async
// version 1 - every
if (!await conditionChecks.every(async (check) => {
const result = await check();
if (!result.passing) {
return false;
}
if (result.key && result.value !== undefined) {
satisfiedConditions[result.key] = surveyGizmoSafe(result.value);
}
return true;
})) {
return failureRes;
}
// version 2 - reduce
const satisfiedConditions = await conditionChecks.reduce(
async (output, check) => {
if (!output) {
return false;
}
const result = await check();
if (!result.passing) {
return false;
}
if (result.key && result.value !== undefined) {
output[result.key] = surveyGizmoSafe(result.value);
}
return output;
},
{}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment