1.0.2 • Published 4 years ago

@wenye123/async-once v1.0.2

Weekly downloads
1
License
ISC
Repository
github
Last release
4 years ago

async-once

Build Status Coverage Status npm npm

多个异步函数只执行一次

安装

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"]
})();