UNPKG

1.75 kBTypeScriptView Raw
1// Type definitions for async-retry 1.4
2// Project: https://github.com/vercel/async-retry
3// Definitions by: Albert Wu <https://github.com/albertywu>
4// Pablo Rodríguez <https://github.com/MeLlamoPablo>
5// Rafał Sawicki <https://github.com/rafsawicki>
6// BendingBender <https://github.com/BendingBender>
7// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
8
9import { WrapOptions } from 'retry';
10
11/**
12 * Retrying made simple, easy, and async.
13 *
14 * @example
15 * import retry = require('async-retry');
16 * import fetch from 'node-fetch';
17 *
18 * await retry(
19 * async (bail) => {
20 * // if anything throws, we retry
21 * const res = await fetch('https://google.com');
22 *
23 * if (403 === res.status) {
24 * // don't retry upon 403
25 * bail(new Error('Unauthorized'));
26 * return;
27 * }
28 *
29 * const data = await res.text();
30 * return data.substr(0, 500);
31 * },
32 * {
33 * retries: 5,
34 * }
35 * );
36 */
37declare function AsyncRetry<TRet>(fn: AsyncRetry.RetryFunction<TRet>, opts?: AsyncRetry.Options): Promise<TRet>;
38
39declare namespace AsyncRetry {
40 interface Options extends WrapOptions {
41 /**
42 * An optional function that is invoked after a new retry is performed. It's passed the
43 * `Error` that triggered it as a parameter.
44 */
45 onRetry?: ((e: Error, attempt: number) => any) | undefined;
46 }
47
48 /**
49 * @param bail A function you can invoke to abort the retrying (bail).
50 * @param attempt The attempt number. The absolute first attempt (before any retries) is `1`.
51 */
52 type RetryFunction<TRet> = (bail: (e: Error) => void, attempt: number) => TRet | Promise<TRet>;
53}
54
55export = AsyncRetry;