~/items/snippets
sleep.ts
snippet · typescript · updated 2d ago
1export const sleep = (ms: number) =>2 new Promise((r) => setTimeout(r, ms));3 4export async function retry<T>(fn: () => Promise<T>, tries = 3) {5 for (let i = 1; ; i++) {6 try {7 return await fn();8 } catch (err) {9 if (i === tries) throw err;10 await sleep(250 * i);11 }12 }13}
42 saved
Snippets
Reusable code with syntax highlighting, language detection and one-click copy.