-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.ts
34 lines (30 loc) · 986 Bytes
/
util.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
export function delay(delay: number = 5000, fn = () => { }, context = null) {
let ticket = null;
return {
run(...args: any[]) {
return new Promise((resolve, reject) => {
ticket = setTimeout(async () => {
try {
const res = await fn.apply(context, args);
resolve(res);
} catch (err) {
reject(err);
}
}, delay);
});
},
cancel: () => {
clearTimeout(ticket);
}
};
};
export const isObject = value => value !== null &&
(typeof value === 'object' || typeof value === 'function');
export function isPromise(value: any) {
return value instanceof Promise ||
(
isObject(value) &&
typeof value.then === 'function' &&
typeof value.catch === 'function'
);
}