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

p-control

v1.1.3

Published

promise concurrency control easy to use. async task control

Downloads

25

Readme

p-control

Promise concurrency control.

Run multiple async tasks with limited number in a easy way.

Why you need this?

When you have many asynchronous tasks, example for 100 http requests, you need to group them and execute them in concurrently group by group, it can help with well.

features

  • control async task by concurrent number
  • api is elegant and easy to use
  • promise and ts based
  • support browser and nodejs
  • very small size
  • MIT license

install

npm install p-control

usage

esm

import pControl from 'p-control';

// control concurrent number,default is 6
// because of browser limit http request number is 6 in one domain
const asyncControl = pControl(2)

const taskParams = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

// async task
const task = params => {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(params)
    }, 1000)
  })
}

// add task to control with params firstly
taskParams.forEach(params => {
  // add task params will be passed to task function
  asyncControl.add(task, params)
})
// then start all tasks
asyncControl
  .start((res, doneSize) => {
    // current concurrent tasks is done
    // res is current concurrent tasks results
    // doneSize is tasks finished size
    // [{index:0, result: 1},{index:1, result: 2}] 2
    // [{index:2, result: 3},{index:3, result: 4}] 4
    // ...
    console.log(res, doneSize)
  })
  .then(allTaskResults => {
    // all tasks is done
    console.log(allTaskResults) // [1,2,3,4,5,6,7,8,9,10]
  })

node commonjs

const pControl = require('p-control')
// same as esm

browser

<!-- umd -->
<script src="https://cdn.jsdelivr.net/npm/p-control/dist/index.umd.js"></script>
<script>
  const pControl = window.PControl // or PControl
 // same as before
</script>
<!-- esm  -->
<script type="module">
  import pControl from 'https://unpkg.com/p-control/dist/index.js';
  // same as before
</script>

API

pControl(limit: number = 6): AsyncControl

Create a async control instance with max concurrent limited number.

AsyncControl

Have two methods:

add(task: Function, params: any): void

Add a task to control with params.

start(concurrentDone?: (res: {index:number,result:any }[], doneSize: number) => void): Promise<any[]>

Start all tasks, concurrentDone will be called when current concurrent tasks is done.

concurrentDone will be called with two arguments:

  • res: current concurrent tasks results, it's an array with object {index: number, result: any}, index is index of current task done and result is current async task result.
  • doneSize: tasks finished size.

You can use res to do something when current concurrent tasks is done, and doneSize to know how many tasks is done. You can calculate progress bar with res or doneSize.

Start return a promise, when all tasks is done, promise will be resolved.

You can use then to get all tasks results.

License

MIT