export function deadline(milliseconds: number, fn: () => Promise) { return new Promise((resolve, reject) => { const id = setTimeout( () => reject(new Error("timeout error")), milliseconds, ) fn() .then(resolve) .catch(reject) .finally(() => clearTimeout(id)) }) }