UNPKG

@prelude/function

Version:

Function module.

18 lines (16 loc) 411 B
const timeout = <T, U>(wait: number, f: () => Promise<T>, g: () => U): Promise<T | U> => new Promise((resolve, reject) => { const id = setTimeout(() => { try { resolve(g()) } catch (err: unknown) { reject(err) } }, wait) f() .finally(() => clearTimeout(id)) .then(resolve) .catch(reject) }) export default timeout