@wenye123/async-once
v1.0.2
Published
多个异步函数只执行一次
Downloads
3
Readme
async-once
多个异步函数只执行一次
安装
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"]
})();