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 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-promise-chain

v1.0.1

Published

## 用于解决node并发异步数据原子性问题

Downloads

5

Readme

node-promise-chain

用于解决node并发异步数据原子性问题

(将并发的请求缓存起来,达到设定数量或者时间后,批量执行)

例如:数据库中有一字段 count: 1

现有累加方法 cumulation,功能是将 count 字段 +1

// 模拟用户请求,10次并发
for (let i = 0; i < 10; i++) {
  cumulation();
}

async function cumulation(query) {
  const data = await db.find(query)
  data.count++
  await db.update(data)
}

当同时有10个请求 cumulation() * 10,最终数据库保存结果为 count: 2

使用

const promiseChain = new PromiseChain(cumulation);

for (let i = 0; i < 10; i++) {
  //将原有调用 cumulation 的地方改为 push
  promiseChain.push("only_one_key", query);
}

async function cumulation(key, tasks) {
  // 并发请求会被收集合并为数组传入
  for (let i = 0; i < task.length; i++) {
    let data = await db.find(query);
    data.count++;
    await db.update(data);
  }
}

会为合并请求例如当请求100次或者5秒内无请求的时候的时候

您也可以修改这个配置

const promiseChain = new PromiseChain({ maxCount: 10, maxTime: 1000, callback: cumulation });

// key 用于区分不同的类型
// args 会被收集保存为数组
promiseChain.push(key, args)

高级用法

使用不同的key来区分合并

const promiseChain = new PromiseChain(cumulation);

for (let i = 0; i < 10; i++) {
  //将原有调用 cumulation 的地方改为 push
  promiseChain.push(query.id, query);
}

async function cumulation(key, tasks) {
  // 并发请求会被收集合并为数组传入
  for (let i = 0; i < task.length; i++) {
    let data = await db.find(query);
    data.count++;
    await db.update(data);
  }
}