Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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Λ }
Add Comment
Please, Sign In to add comment