-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpromises.ts
More file actions
39 lines (33 loc) · 1.1 KB
/
promises.ts
File metadata and controls
39 lines (33 loc) · 1.1 KB
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
import pMinDelay from "p-min-delay";
/**
* Setting up a minimum wait time allows users
* to visually grasp the text before it goes away, if
* the task was faster than the minimum wait time.
*/
const MIN_TIME_BETWEEN_STEPS_MS = 500;
/**
* Waits at least {@link MIN_TIME_BETWEEN_STEPS_MS} before resolving.
*/
export function minDelay(): Promise<void>;
/**
* Executes the callback, and waits at least {@link MIN_TIME_BETWEEN_STEPS_MS} before resolving.
*/
export function minDelay<T>(callback: () => PromiseLike<T>): Promise<T>;
/**
* Waits for the given promise, with a minimum wait of at least {@link MIN_TIME_BETWEEN_STEPS_MS}.
*/
export function minDelay<T>(promise: PromiseLike<T>): Promise<T>;
export function minDelay<T>(
promiseOrCallback?: PromiseLike<T> | (() => PromiseLike<T>),
): Promise<T> {
return pMinDelay(
typeof promiseOrCallback === "function"
? promiseOrCallback()
: (promiseOrCallback ?? (Promise.resolve() as Promise<T>)),
MIN_TIME_BETWEEN_STEPS_MS,
);
}
/**
* Extracts the resolved type from a Promise.
*/
export type UnwrapPromise<T> = T extends Promise<infer U> ? U : T;