多个异步函数只执行一次
npm i -S @wenye123/async-once
import AsyncOnce from "@wenye123/async-once";
function sleep(ms: number) {
return new Promise(resolve => {
setTimeout(resolve, ms);
});
}
(async function() {
const aonce = new AsyncOnce({ timeout: 3000 });
const tag = "test";
let counter = 0;
async function getData() {
counter++;
await sleep(50);
return "wenye";
}
const ret = await Promise.all([
aonce.once(tag, getData),
aonce.once(tag, getData),
aonce.once(tag, getData),
aonce.once(tag, getData),
]);
console.debug(counter); // 1
console.debug(ret); // ["wenye", "wenye", "wenye", "wenye"]
})();