@botonic/plugin-contentful
Version:
Botonic Plugin Contentful is one of the **[available](https://github.com/hubtype/botonic/tree/master/packages)** plugins for Botonic. **[Contentful](http://www.contentful.com)** is a CMS (Content Management System) which manages contents of a great variet
45 lines (41 loc) • 1 kB
text/typescript
export function sleep(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms))
}
export interface Backoff {
backoff(): Promise<void>
}
export class ExponentialBackoff implements Backoff {
constructor(
private startMs = 10,
private times = 10
) {}
async backoff(): Promise<void> {
if (this.times <= 0) {
throw new Error('Aborting exponential backoff')
}
await sleep(this.startMs)
this.startMs *= 2
this.times++
}
}
export async function repeatWithBackoff<T>(
func: () => Promise<T>,
backoff = new ExponentialBackoff(),
logger: (msg: string) => void = console.log
): Promise<T> {
for (;;) {
try {
return await func()
} catch (e: any) {
const stack = e.stack
? `\nat:\n${String(e.stack)}`
: ' (no stack available)'
logger(
`Retrying after exception at ${new Date().toISOString()}: ${String(
e
)}` + stack
)
await backoff.backoff()
}
}
}