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

thunk-workers

v1.2.0

Published

Thunk-based task scheduler that executes synchrounous and/or asynchronous tasks under concurrency control.

Downloads

17

Readme

thunk-workers

Thunk-based task scheduler that executes synchrounous and/or asynchronous tasks under concurrency control.

NPM version Build Status Downloads

thunks

Demo

const thunk = require('thunks')()
const thunkWorkers = require('thunk-workers')
const workshop = thunkWorkers(2)

for (let i = 1; i <= 10; i++) addWork(i)

function addWork (id) {
  workshop(function () {
    console.log('Task ' + id + ' start:')

    let timer = setInterval(function () {
      process.stdout.write(String(id))
    }, 50)

    let time = 200 + Math.floor(1000 * Math.random())
    if (id === 10) time = 10

    return thunk.delay(time)(function () {
      clearInterval(timer)
      return time
    })
  })(function (error, res) {
    console.log(error || ('\nTask ' + id + ' finished, ' + res + ' ms.'))
  })
}

// Result:
// Task 1 start:
// Task 2 start:
// 121212121212121212
// Task 2 finished, 510 ms.
// Task 3 start:
// 13131313131313131
// Task 1 finished, 981 ms.
// Task 4 start:
// 343434343434343434343434343
// Task 3 finished, 1199 ms.
// Task 5 start:
// 45454545454
// Task 4 finished, 1031 ms.
// Task 6 start:
// 565656565656565656
// Task 6 finished, 485 ms.
// Task 7 start:
// 5757575757
// Task 5 finished, 1072 ms.
// Task 8 start:
// 78787878787
// Task 8 finished, 317 ms.
// Task 9 start:
// 7979797979
// Task 7 finished, 886 ms.
// Task 10 start:
// Task 9 finished, 284 ms.
// Task 10 finished, 10 ms.

API

const thunkWorkers = require('thunk-workers')

thunkWorkers([count])

Create a workshop that limits the number of concurrent tasks being executed.

  • count: {Number} Maximum number of task threads being executed concurrently. Default to 1.
const workshop = thunkWorkers(5)

workshop(task)

Return a thunk function that executes a specific task. Tasks are queued by the time the returned thunk function is executed. Once the number of concurrent tasks is within workshop's limitation, a task is polled from the queue and executed.

  • task: {Function} Support sync task or async task, task must be a function or a generator function. Async task should be thunkable function, or return a thunkable value, such as thunk function, promise, generator function, generator object.
// Support thunk function
const job = workshop(function (callback) {
  setTimeout(function () {
    callback(null, 'Async task')
  }, 100)
})

job(function (err, res) {
  console.log(err, res)
})

// Support Promise
workshop(function () {
  return Promise.resolve('promise')
})(function (err, res) {
  console.log(err, res)
})

// Support Generator function
workshop(function * () {
  yield thunk.delay(100)
  console.log('Generator task')
})(function (err, res) {
  console.log(err, res)
})

//Support async/await function
workshop(async function () {
  await Promise.resolve()
  console.log('async/await task')
})(function (err, res) {
  console.log(err, res)
})