Retry joinRoom up to 5 times in the case of a 504 GATEWAY TIMEOUT

This commit is contained in:
Michael Telatynski 2020-09-14 15:16:29 +01:00
parent c68a980c59
commit 229967aa33
2 changed files with 37 additions and 6 deletions

View file

@ -68,3 +68,21 @@ export function allSettled<T>(promises: Promise<T>[]): Promise<Array<ISettledFul
}));
}));
}
// Helper method to retry a Promise a given number of times or until a predicate fails
export async function retry<T, E extends Error>(fn: () => Promise<T>, num: number, predicate?: (e: E) => boolean) {
let lastErr: E;
for (let i = 0; i < num; i++) {
try {
const v = await fn();
// If `await fn()` throws then we won't reach here
return v;
} catch (err) {
if (predicate && !predicate(err)) {
throw err;
}
lastErr = err;
}
}
throw lastErr;
}