npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

simple-async-combine

v1.0.2

Published

异步合并方法

Readme

simple-async-combine

异步调用合并。当多个调用者同时发起相同参数的异步请求时,只执行一次,所有调用者共享同一结果。

典型场景:

  • 多个组件同时请求同一份接口数据
  • 防止重复的并发 API 调用
  • 对并发异步操作进行去重

安装

npm i simple-async-combine

API

asyncCombine(fn, options?)

将一个异步函数包装为可合并调用的版本。

参数:

  • fn - 需要包装的异步函数
  • options.ignoreArguments - boolean,默认 false。设为 true 时,忽略参数差异,所有并发调用都会被合并为一次执行

返回值: 与原函数签名相同的包装函数,调用后返回 Promise

使用示例

相同参数的并发调用

多次传入相同参数时,函数只执行一次,所有调用者拿到相同结果。

import asyncCombine from 'simple-async-combine';

let count = 0;
let _args: number[] = [];

const handle = asyncCombine((...args: number[]) => new Promise(resolve => {
  setTimeout(() => {
    _args = args;
    count++;
    resolve('aa');
  }, 2000);
}));

const results = await Promise.all([handle(1, 2), handle(1, 2)]);

// count === 1              // 函数只执行了一次
// results === ['aa', 'aa'] // 两个调用者拿到了相同结果
// _args === [1, 2]

忽略参数差异

开启 ignoreArguments 后,即使参数不同也会合并为一次调用。执行时使用第一次调用的参数。

import asyncCombine from 'simple-async-combine';

let count = 0;
let _args: number[] = [];

const handle = asyncCombine((...args: number[]) => new Promise(resolve => {
  setTimeout(() => {
    _args = args;
    count++;
    resolve('aa');
  }, 2000);
}), { ignoreArguments: true });

const results = await Promise.all([handle(1, 2), handle(2, 3)]);

// count === 1              // 参数不同也被合并
// results === ['aa', 'aa']
// _args === [1, 2]         // 使用第一次调用的参数

混合参数的并发调用

不同参数的调用各自分组合并,相同参数共享同一次执行。

import asyncCombine from 'simple-async-combine';

let count = 0;
const _args: number[][] = [];

const handle = asyncCombine((...args: number[]) => new Promise(resolve => {
  setTimeout(() => {
    _args.push(args);
    count++;
    resolve(args);
  }, 2000);
}));

const results = await Promise.all([
  handle(1, 2),
  handle(1, 2),
  handle(2, 3),
]);

// count === 2                            // 分为两组: [1,2] 和 [2,3]
// results === [[1, 2], [1, 2], [2, 3]]  // 每个调用者拿到对应结果
// _args === [[1, 2], [2, 3]]            // 实际执行了两次

License

MIT