Skip to content

Instantly share code, notes, and snippets.

View dperuo's full-sized avatar

Derek Peruo dperuo

View GitHub Profile
@dperuo
dperuo / backoff.ts
Last active January 16, 2024 15:49
const backoff = (config?: { readonly scale?: number; readonly count?: number; }): readonly number[] => {
return [...Array(config?.count ?? 4).keys()].map(item => 2 ** item * (config?.scale ?? 1));
}
const backoff$ = (config?: { readonly delay?: number; readonly count?: number; readonly scale?: number;}) => {
return from([...Array(config?.count ?? 4).keys()]).pipe(
concatMap(next => timer((config?.scale ?? 2) ** next * (config?.delay ?? 500)))
)
}