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

easy-async

v1.0.0

Published

a library to manage ansynchronous control flow, similar to Promises/A+/q, but not exactly the same.

Downloads

7

Readme

easy-async

easy-async is a library that helps coordinate asynchronous tasks, similar to Promises and async.js.

Like Promises, you get a "control object" that wraps an asynchronous task. The control object allows you to declare what to do after (or at the same time) as the wrapped task.

Like async.js, easy-async will NOT attempt to plumb arguments through your tasks. Also like async.js, the tasks will fail fast if any task produces an error.

Examples

You can see the examples together in a script here

Tasks receive only one parameter, the callback provided by easy-async. Each task must call the callback, and easy-async will only look for one single argument and assume it's an error.

var exampleTask = function(callback){
  console.log('starting task');
  setTimeout(function(){
    console.log('done with task');
    callback();
  }, 1000);
}

var exampleErrorTask = function(callback){
  console.log('starting task');
  setTimeout(function(){
    callback(new Error('BOOM!'));
  }, 1000);
};

To run through tasks in series, get the control object with 'start', then line up tasks with the 'thenStart' method on the control object:

easyAsync.start(exampleTask)
.thenStart(exampleTask)
.thenStart(exampleTask)
.thenStart(function() {
  console.log('continuing after tasks in series');
});

To run tasks in parallel, use the 'andStart' method on the control object:

easyAsync.start(exampleTask)
.andStart(exampleTask)
.andStart(exampleTask)
.thenStart(function() {
  console.log('continuing after tasks in parallel');
});

To attach an error handler, use the 'onError' method on the control object:

easyAsync.start(exampleTask)
.andStart(exampleTask)
.andStart(exampleErrorTask)
.thenStart(function() {
  console.log('continuing after risky work');
})
.onError(function(err){
  console.log('error handler received an error');
  console.error(err);
});

API

The easy-fix module exports only one method: start. The start method returns the "control object" (kind of like a promise) which contains the other methods listed here.

start(callback, options)

Create and return the control object, and start the callback task. The control object has the other methods listed here. The options are the same as thenStart and andStart methods (listed below). As a convenience, the callback can be null.

The callback is called with exactly one argument (a callback) which it must call to continue the easy-fix asynchronous task execution.

thenStart(callback, options)

Run the callback after all previous callbacks to thenStart and andStart have completed. Use this to declare tasks to run in series.

The callback is called with exactly one argument (a callback) which it must call to continue the easy-fix asynchronous task execution.

andStart(callback, options)

Run the callback in parallel with a previous callback from thenStart, as well as any other other callbacks from successive calls to andStart. Use this to declare tasks to execute in parallel.

The callback is called with exactly one argument (a callback) which it must call to continue the easy-fix asynchronous task execution.

onError(callback)

Specify a custom error handler. The default error handler simply throws the error.

The error handler callback is called with two arguments: (error, errorIndex).

The error is the same error that was passed in to any task callback or caught by a try/catch block (when wrapWithTry is specified as an option).

The errorIndex is a count (starting at 1) of all errors encountered by this control object. As an example use case, easy-async may be used to coordinate multiple tasks in parallel when preparing a response to a single web request. Several of the tasks may generate errors - and the errorIndex may be used to guarantee that the error handler will only attempt to respond once to that web request.

changeDefaults(options)

Specify new default options, which apply to all futher tasks added with thenStart and andStart.

Options

Options can be specified per-task, or by calling the changeDefaults method.

  • wrapWithTry - If true, tasks will be wrapped in a try/catch block, and caught errors passed through the onError handler. Default is false.
  • onError - specify a custom error handler. The default error handler simply throws the error.

Stay up to date

As of June 2016, the new easy-async v1 has some very prudent breaking changes (and new features)! Please read the change log!