Last active
April 16, 2018 03:19
-
-
Save VitorLuizC/11b6d160965cb38ab15b85a7c1a7f5d3 to your computer and use it in GitHub Desktop.
Insistence module.
This file contains hidden or 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
| const wait = (time: number) => new Promise((resolve) => setTimeout(resolve, time)); | |
| type InsistΛ = () => boolean | Promise<boolean>; | |
| const insist = (λ: InsistΛ, time: number = 200): Promise<void> => { | |
| return new Promise((resolve) => { | |
| const insist = () => Promise.resolve(λ()) | |
| .then((success: boolean) => { | |
| if (!success) | |
| throw new Error('Success not achieved. Keep insisting.'); | |
| resolve(); | |
| }) | |
| .catch(() => wait(time).then(insist)); | |
| return insist(); | |
| }); | |
| }; | |
| export { insist as default, InsistΛ } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment