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

promise-formation

v1.0.6

Published

Promise.all Like function to manage promise queue

Downloads

4

Readme

promise-formation

Document

https://github.com/zobor/promise-formation

promise-fomation 提供类似 Promise.all 的功能,拓展了并发控制的能力。

Promise.all 的异步执行是不能控制数量和顺序的,promise-fomation 针对这些做了优化。

场景是响应操作前、过程中、后的各种用户操作。支持控制并发允许的 Promise 数量,支持异步添加任务,支持异步停止任务。

PromiseFormation VS Promise.all

| | Promise.all | PromiseFormation | | ---------------- | ----------- | ---------------- | | 并行执行异步函数 | ✔ | ✔ | | 控制任务顺序 | ✘ | ✔ | | 限制并发数量 | ✘ | ✔ | | 动态添加任务 | ✘ | ✔ | | 动态停止任务 | ✘ | ✔ | | 停止指定的任务 | ✘ | ✔ |

安装

$ npm install promise-formation -S

代码运行对比

Promise.all

const p1 = new Promise(resolve => {
  setTimeout(() => {
    console.log(1);
    resolve("A");
  }, 3);
});
const p2 = new Promise(resolve => {
  setTimeout(() => {
    console.log(2);
    resolve("B");
  }, 2);
});
const p3 = new Promise(resolve => {
  setTimeout(() => {
    console.log(3);
    resolve("C");
  }, 1);
});

Promise.all([p1, p2, p3]).then(rs => {
  console.log(rs);
});
// 3
// 2
// 1
// A B C

Promilse.formation

import promiseFormation from "promise-formation";

const p1 = () =>
  new Promise(resolve => {
    setTimeout(() => {
      console.log(1);
      resolve("A");
    }, 3);
  });
const p2 = () =>
  new Promise(resolve => {
    setTimeout(() => {
      console.log(2);
      resolve("B");
    }, 2);
  });
const p3 = () =>
  new Promise(resolve => {
    setTimeout(() => {
      console.log(3);
      resolve("C");
    }, 1);
  });

promiseFormation([p1, p2, p3]).then(rs => {
  console.log(rs);
  // 1
  // 2
  // 3
  // ["A", "B", "C"]
});

用法

import promiseFormation from "promise-formation";

const getPromises = (len: number): any =>
  Array(len)
    .fill(null)
    .map((_, index) => () =>
      new Promise(async resolve => {
        await new Promise(rs => {
          setTimeout(() => {
            rs();
          }, 1000);
        });
        resolve(index + 1);
      })
    );

promiseFormation(getPromises(3)).then((res: number[]) => {
  console.log(res);
});

// 输出
[1, 2, 3];

设置并发数量

const getPromises = (len: number): any =>
  Array(len)
    .fill(null)
    .map((_, index) => () =>
      new Promise(async resolve => {
        await new Promise(rs => {
          setTimeout(() => {
            rs();
          }, 1000);
        });
        resolve(index + 1);
      })
    );
promiseFormation(getPromises(9), 3).then((res: number[]) => {
  console.log(res);
});

// 输出
1;
2;
3;

4;
5;
6;

7;
8;
(9)[(1, 2, 3, 4, 5, 6, 7, 8, 9)];

异步添加任务

const promiseOptions: any = {};
promiseFormation(getPromises(2), 1, promiseOptions).then((res: number[]) => {
  console.log(res);
});
promiseOptions.addOne(getPromises(1)[0]);

// 输出
[1, 2, 1];

异步停止任务

const isError = v => Object.prototype.toString.call(v) === "[object Error]";
const promiseOptions: any = {};
const p = getPromises(3).map((fn: any, index: number) => {
  fn.id = `test-${index + 1}`;
  return fn;
});
promiseFormation(p, 1, promiseOptions).then((res: any[]) => {
  console.log(res.length);
  console.log(isError(res[1]));
});
promiseOptions.stopOne("test-2");

// 输出
3;
true;

参数

type promiseListItem = (value: void) => Promise<any>;

promiseFormation(promiseList, max, options);
  • promiseList: promiseListItem[] 任务列表
  • max: number 最大并行任务数,默认 1
  • options
    • options.addOne: (promiseListItem) => vid 添加一个任务
    • options.stop: boolean 执行过程中,修改options.stop为 true 之后 后面的任务全部立即返回失败(闭包内部的实参是形参的引用)
    • options.stopOne: (taskId: string) => void 指定要停止的任务