foxts
Version:
Opinionated collection of common TypeScript utils by @SukkaW
1 lines • 459 B
JavaScript
;exports.waitFor=function(t,o=50,e=null){if("object"==typeof o&&"aborted"in o&&(e=o,o=50),"number"==typeof o){const t=o;o=(o,e)=>{const n=setTimeout(o,t);e(()=>clearTimeout(n))}}e??=AbortSignal.timeout(5e3);const n=new Set,r=t=>n.add(t);return new Promise((c,i)=>{const a=async()=>{try{e.throwIfAborted();const i=await t();if(i)return void c(i);const s=o(a,r);"function"==typeof s&&n.add(s)}catch(t){i(t)}};a()}).finally(()=>{n.forEach(t=>t())})};