@sconedev/ai_toolkit
Version:
Simplify AI integration in web apps with local and offline model support
17 lines (16 loc) • 570 B
JavaScript
export async function retry(fn, retries) {
let lastError;
for (let i = 0; i < retries; i++) {
try {
return await fn();
}
catch (error) {
lastError = error;
if (i === retries - 1) {
throw lastError; // Throw the last error if all retries fail
}
// Optionally, add a delay here for backoff (e.g., await new Promise(resolve => setTimeout(resolve, 1000)))
}
}
throw lastError; // This line is for TypeScript to ensure all code paths return a value
}