mirror of
https://github.com/certd/certd.git
synced 2026-05-16 13:17:29 +08:00
14 lines
347 B
TypeScript
14 lines
347 B
TypeScript
|
|
export function TimeoutPromise(callback: () => Promise<void>, ms = 30 * 1000) {
|
||
|
|
let timeout: any;
|
||
|
|
return Promise.race([
|
||
|
|
callback(),
|
||
|
|
new Promise((resolve, reject) => {
|
||
|
|
timeout = setTimeout(() => {
|
||
|
|
reject(new Error(`Task timeout in ${ms} ms`));
|
||
|
|
}, ms);
|
||
|
|
}),
|
||
|
|
]).finally(() => {
|
||
|
|
clearTimeout(timeout);
|
||
|
|
});
|
||
|
|
}
|