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

asynccond

v1.1.0

Published

async series with conditional exit

Downloads

4

Readme

asynccond

async series with conditional exit

NPM version Build Status

Async series functions with the ability to conditionally pre-exit the sequence.

seq furthermore allows to trap errors within the sequence.

The functions provided are backwards compatible with async.

Table of Contents

Description

eachSeries(arr, iterator, callback)

Applies the function iterator to each item in arr in series. The iterator is called with an item from arr, and a callback when it has finished. If the iterator passes an error to its callback, the main callback is immediately called with the error.

If a condition is applied on the internal callback of iterator, it pre-exits the series.

Example

eachSeries(
  [ 1, 2, 3, 4, 5 ],
  // iterator
  function (data, cb) {
    cb(null, data * 2, (data * 2 > 5)); // exits if condition is met
  },
  // callback
  function(err, data){
    //> data = [ 2, 4, 6 ]);
  }
);

Parameters

arr: Array, array of items which are passed to iterator

iterator: function, function(item, cb) cb needs to be called inside iterator

callback: function, is of type function(err, result) and called after running the series

series(tasks, callback)

Run the functions in the tasks array in series, each one running once the previous function has completed. If any functions in the series pass an error to its callback, no more functions are run, and callback is immediately called with the value of the error. Otherwise, callback receives an array of results when tasks have completed.

It is also possible to pass an object instead of an array for tasks. Each property will be run as a function, and the results will be passed to the final callback as an object instead of an array. This can be a more readable way of handling results from series.

The series can be exited immediately on any internal callback.

Example

var series = require('asynccond').series;
var data = 7;

series({
  one: function(cb) {
    cb(null, data+=10);
  },
  two: function(cb) {
    cb(null, data+=10, (data>25)); // conditional exit
  },
  three: function(cb) {
    cb(null, data+=10);
  }
},function(err, result) {
  //> result = { one: 17, two: 27 }
})

Parameters

tasks: Array | Object, the async functions to run in series

callback: function, is of type function(err, result) and called after running the series

seq(tasks)

Creates a function which is a composition of the passed asynchronous functions. Each function consumes the return value of the function that follows.

Each function is executed with the this binding of the composed function.

Example

var seq = require('asynccond').seq;
var start = 1;

seq(
  function(data, cb) {
    cb(null, data+1);
  },
  function(data, cb) {
    cb('err', data+1); // causes an error --> will execute the next function with arity = 3
  },
  function(data, cb) {
    cb(null, data+1);  // jumps over here (remind the previous error)
  },
  function(err, data, cb) { // the error trap (arity = 3) is only called if there is an error
    cb(null, data+10);
  },
  function(data, cb) {
    cb(null, data+1, (data > 10)); // exits list immediately if data>10
  },
  function(data, cb) {
    cb(null, data+1); // never reaches in this case
  }
)(
  start,
  function(err, result) {
    // the final callback function
    //> err    = null
    //> result = 14
  }
);

Parameters

tasks: Array | Object | function, Array or Object or Arguments list of functions function(data, callback) where callback is of type function(err, result, [exit])

Returns: function, call with (data, callback) where callback is of type function(err, result)

Contribution and License Agreement

If you contribute code to this project, you are implicitly allowing your code to be distributed under the MIT license. You are also implicitly verifying that all code is your original work or correctly attributed with the source of its origin and licence.

License

Copyright (c) 2015 commenthol (MIT License)

See LICENSE for more info.

References