Skip to content

wenye123/async-once

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

多个异步函数只执行一次

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published