Skip to content

Instantly share code, notes, and snippets.

@VitorLuizC
Last active April 16, 2018 03:19
Show Gist options
  • Select an option

  • Save VitorLuizC/11b6d160965cb38ab15b85a7c1a7f5d3 to your computer and use it in GitHub Desktop.

Select an option

Save VitorLuizC/11b6d160965cb38ab15b85a7c1a7f5d3 to your computer and use it in GitHub Desktop.
Insistence module.
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