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

async-control

v0.5.0

Published

Ultimate asynchronous control flow goodness with built-in hook system and compose, series, define and parallel methods. Uses async.map and async.mapSeries methods. Allows passing custom iterator function.

Downloads

20

Readme

async-control npmjs.com The MIT License

Ultimate asynchronous control flow goodness with built-in hook system and compose, series, define and parallel methods. Uses async.map and async.mapSeries methods. Allows passing custom iterator function.

code climate standard code style travis build status coverage status dependency status

Install

npm i async-control --save

Usage

For more use-cases see the tests

const fs = require('fs')
const asyncControl = require('async-control')

asyncControl.series([
  function one (next) {
    console.log('first')
    fs.readFile('package.json', 'utf8', next)
  },
  function two (next) {
    setTimeout(function () {
      console.log('second')
      fs.stat('not exist', next)
    }, 100)
  },
  function three (next) {
    console.log('third')
    next(null, 123)
  }
], {settle: false}, function done (err, res) {
  // => first
  // => second
  // => third

  console.log('err:', err)
  // => if `options.settle` is true - `undefined`, otherwise thrown Error
  console.log('res:', res) // => array of results
  // => res[0] is content of package.json file
  // => res[1] is `undefined`, because second function throws Error (ENOENT)
  // if `options.settle` is true
  // - res[1] will be Error
  // - res[2] will be 123
})

AsyncControl

Initialize AsyncControl with options to control enabling/disabling options.settle, passing custom iterator and pass different hooks - before, after, etc.

Params

  • options {Object=}

.series

Iterate over value in series flow. The async.mapSeries method is used.

Params

  • value {Object|Array|Function}: The value to iterate over.
  • options {Object=}: Can pass different hooks - before, after, beforeEach, afterEach.
  • done {Function=}: If not passed, thunk is returned (function that accepts callback).
  • returns {Function}: Or undefined if done is passed.

Example

var fs = require('fs')
var asyncControl = require('async-control')

asyncControl.series([
  function one (cb) {
    cb(null, 'foo')
  },
  function two (cb) {
    fs.readFile('not exist', cb)
  },
  function three (cb) {
    cb(null, 'bar')
  }
], console.log) //=> ENOENT Error, ['foo', undefined]

.parallel

Iterate over value in parallel flow. The async.map method is used.

Params

  • value {Object|Array|Function}: The value to iterate over.
  • options {Object=}: Can pass different hooks - before, after, beforeEach, afterEach.
  • done {Function=}: If not passed, thunk is returned (function that accepts callback).
  • returns {Function}: Or undefined if done is passed.

Example

var fs = require('fs')
var asyncControl = require('async-control')

asyncControl.parallel([
  function one (next) {
    setTimeout(function () {
      console.log('first')
      next(null, 100)
    }, Math.random() * 50)
  },
  function two (next) {
    setTimeout(function () {
      console.log('second')
      next(null, 700)
    }, Math.random() * 200)
  },
  function three (next) {
    setTimeout(function () {
      console.log('third')
      next(null, 2000)
    }, 0)
  }
], function done (err, res) {
  // => third
  // => first
  // => second

  console.log(err) // => null
  console.log(res) // => [2000, 100, 700]
})

.compose

Compose series or parallel method. Can be used to create settleSeries or settleParallel methods for example.

Params

  • flow {String}: Type of flow, one of 'series' or 'parallel'.
  • options {Object=}: Can pass different hooks - before, after, beforeEach, afterEach.
  • returns {Function}: Composed series or parallel method, depends on flow.

Example

var fs = require('fs')
var asyncControl = require('async-control')

// the internal `.series` method is created this way - using `.compose`
var series = asyncControl.compose('series')
series([
  function one (cb) {
    cb(null, 123)
  },
  function two (cb) {
    fs.readFile('not exist', cb)
  },
  function three (cb) {
    cb(null, 456)
  }
], {settle: true}, console.log) //=> null, [123, ENOENT Error, 456]

Custom iterator

Below example shows how to create your own iterator using asyncControl.define method. This also can be done by passing function to options.iterator. It recieves app and options and must return function which is directly passed to async.map and/or async.mapSeries methods.

Params

  • app {Object}: instance of AsyncControl
  • opts {Object}: the app options
  • returns {Function}: iterator passed directly to async.map / async.mapSeries

Example

const util = require('util')
const asyncControl = require('async-control')

asyncControl.define('iterator', function customIterator (app, options) {
  // this === app
  return iterator (fn, next) {
    // this === app
    console.log(util.inspect(fn))
    next()
  }
})
asyncControl.series([
  function first (next) { next(null, 1) },
  function second (next) { next(null, 2) },
  function third (next) { next(null, 3) }
], function (err, res) {
  // => [Function: first]
  // => [Function: second]
  // => [Function: third]

  console.log(err, res)
  // => null, [1, 2, 3]
})

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
But before doing anything, please read the CONTRIBUTING.md guidelines.

Charlike Make Reagent new message to charlike freenode #charlike

tunnckoCore.tk keybase tunnckoCore tunnckoCore npm tunnckoCore twitter tunnckoCore github