Skip to content

Instantly share code, notes, and snippets.

@dperuo
Last active January 16, 2024 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dperuo/b179d2a94d1002531ffaa6e19a24e9f3 to your computer and use it in GitHub Desktop.
Save dperuo/b179d2a94d1002531ffaa6e19a24e9f3 to your computer and use it in GitHub Desktop.
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)))
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment