@wenye123/async-once
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

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

Package Sidebar

Install

npm i @wenye123/async-once

Weekly Downloads

0

Version

1.0.2

License

ISC

Unpacked Size

4.8 kB

Total Files

4

Last publish

Collaborators

  • wenye123