Skip to content

Commit 1101cab

Browse files
committed
feat: add later func
1 parent 7c431f7 commit 1101cab

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

src/delay.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,17 @@ import { isFunction } from '.'
22

33
/**
44
* @description 延迟 wait 毫秒后调用 func。 调用时,任何附加的参数会传给func。
5-
* @param {Function} func 要延迟的函数
6-
* @param {number} wait 要延迟的毫秒数
7-
* @param {...*} [args] 会在调用时传入到 func 的参数。
8-
* @returns {number} 返回计时器 id
95
*/
106

11-
export function delay(
12-
func: (...args: any[]) => any,
13-
wait = 0,
14-
...args: any[]
15-
): ReturnType<typeof setTimeout> {
7+
export function delay(func: (...args: any[]) => any, wait = 0, ...args: any[]) {
168
if (!isFunction(func)) {
179
throw new TypeError('Expected a function')
1810
}
1911
return setTimeout(func, wait, ...args)
2012
}
13+
14+
export function later(delay = 0): Promise<void> {
15+
return new Promise((resolve) => {
16+
setTimeout(resolve, delay)
17+
})
18+
}

0 commit comments

Comments
 (0)