-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.d.ts
91 lines (77 loc) · 3.34 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Type definitions for promise-tools
// Project: node-promise-tools
// Definitions by: Calvin Wiebe [email protected]
declare class TimeoutError extends Error {}
type PromiseGeneratingFunction<t> = () => PromiseLike<t>;
/**
* Returns a Promise which resolves after `ms` milliseconds have elapsed. The returned Promise will never reject.
*/
export function delay(ms: number): Promise<void>;
type Deferred<t> = {
promise: Promise<t>;
resolve: (result: t) => t;
reject: (error: any) => any;
};
/**
* Returns a `{promise, resolve, reject}` object. The returned `promise` will resolve or reject when `resolve` or
* `reject` are called.
*/
export function defer<t>(): Deferred<t>;
/**
* Given an array, `tasks`, of functions which return Promises, executes each function in `tasks` in series, only
* calling the next function once the previous function has completed.
*/
export function series<t>(tasks: PromiseGeneratingFunction<t>[]): Promise<t[]>;
/**
* Given an array, `tasks`, of functions which return Promises, executes each function in `tasks` in parallel.
* If `limit` is supplied, then at most `limit` tasks will be executed concurrently.
*/
export function parallel<t>(tasks: PromiseGeneratingFunction<t>[], limit?: number): Promise<t[]>;
type MapIterator<t, u> = (item: t, index: number) => Promise<u>;
/**
* Given an array `arr` of items, calls `iter(item, index)` for every item in `arr`. `iter()` should return a
* Promise. Up to `limit` items will be called in parallel (defaults to 1.)
*/
export function map<t, u>(arr: t[], iter: MapIterator<t, u>, limit?: number): Promise<u[]>;
/**
* Add a timeout to an existing Promise.
*
* Resolves to the same value as `p` if `p` resolves within `ms` milliseconds, otherwise the returned Promise will
* reject with the error "Timeout: Promise did not resolve within ${ms} milliseconds"
*/
export function timeout<t>(p: Promise<t>, ms: number): Promise<t>;
/**
* Continually call `fn()` while `test()` returns true.
*
* `fn()` should return a Promise. `test()` is a synchronous function which returns true of false.
*
* `whilst` will resolve to the last value that `fn()` resolved to, or will reject immediately with an error if
* `fn()` rejects or if `fn()` or `test()` throw.
*/
export function whilst<t>(test: () => boolean, fn: PromiseGeneratingFunction<t>): Promise<t>;
/**
* Same as `whilst` but will call `test()` before trying `fn`
*/
export function doWhilst<t>(fn: PromiseGeneratingFunction<t>, test: () => boolean): Promise<t>;
/**
* Function to be called until resolves by `retry`. It will be passed the `lastAttempt` failure of the previous call.
*/
type RetryTask<t> = (lastAttempt: any) => Promise<t>;
/**
* Options for the retry method
*/
type RetryOptions = number | {
times: number,
interval: number
}
/**
* Will call `fn` 5 times until it resolves. If after 5 tries `fn` still rejects, `retry` will reject
* with the last error
*/
export function retry<t>(fn: RetryTask<t>): Promise<t>;
/**
* Continually call `fn` until it resolves. `retry` will call it `options.times` before giving up and rejecting. The
* default amount of times is 5. You can set it to `Infinity` to try forever. The interval between each retry is 0,
* unless specified in milliseconds in `options.interval`.
*/
export function retry<t>(options: Partial<RetryOptions>, fn: RetryTask<t>): Promise<t>;