Some promise extension functions
use pnpm to install
npm i @zunh/promise-kit
pnpm add @zunh/promise-kit
await withTimeout(Promise.resolve('success'), 1000)
const { resolve, promise } = withResolvers<string>();
resolve('success');
const result = await promise;
await withRetry(async () => {
return 'success';
}, 3, 1000);
await forEach([1, 2, 3], async (item) => {
item + 1;
});
const result = await map([1, 2, 3], async (item) => {
return item + 1;
});
await parallel([1,2,3], async () => {}, 2);
await sleep(1000);
await randomSleep(1000, 2000);
const { run, waitComplete, pause, resume } = createTask([1,2,4])
run(async (item) => {
return item + 1;
})
pause()
resume()
const result = await waitComplete()
MIT License © 2023-PRESENT zhaozunhong