Last active
August 21, 2021 12:32
-
-
Save clovisdasilvaneto/0c1ceb84f1c9c000072ea32f4856eeff 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
async function* iteratableGuards(validators) { | |
let currentValidator = 0 | |
let previousResult = null | |
while (currentValidator < validators.length) { | |
previousResult = yield await validators[currentValidator](previousResult) | |
currentValidator++ | |
} | |
} | |
const iterate = async (iterator, result) => { | |
const { value, done } = await iterator.next() | |
if (done) return result | |
result = [...result, value] | |
iterate(iterator, result) | |
} | |
export const OrderedGuard = async (validators) => { | |
const iteratableValidators = iteratableGuards(validators) | |
return iterate(iteratableValidators, []) | |
} | |
export const RaceGuard = async (guards: any) => { | |
try { | |
const results = await Promise.all(guards.map((guard) => guard())) | |
return results | |
} catch (err) { | |
Promise.reject(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment