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-helper

v1.0.3

Published

This package will read an array of async function responses and create a final, single response with error or success to Express.

Downloads

12

Readme

async-helper

This helper gets an array of async responses and create a final, single response to express.

Each function in async should set the err variable to true if an error occoured so async-helper will use it's response to create the HTML code and create the object that will be used by Express to send the response.

The default HTML code, if no error occoured is succes (200).

Install

In your project root folder, run

$ npm install --save async-helper

Usage

You should call async-helper after the execution of your async functions, for instance:

var asyncHelper = require('async-helper');
/*
 * START SERIAL FUNCTIONS
 */
async.series([
  // Validate arguments
  function(callback) {
    args.validate(
      mobletId, gitUrl, mobletName, mobletVersion,
      gitCheckout, callback);
  },
  // GIT Clone
  function(callback) {
    git.clone(
      mobletId, gitUrl, gitCheckout,
      mobletVersion, callback);
  },
  /*
   * START PARALLELL FUNCTIONS
   */
  function(callback) {
    async.parallel([
      // Test Moblet with Jasmine
      function(callback) {
        moblet.runTests(mobletId, callback);
      },
      // Get Moblet definition
      function(callback) {
        moblet.getDefinitions(mobletId, callback);
      }
    ],
      // Parallel callback function
      callback
    )
  }
],
  function(err, response) {
    /***************************************************************************
    * ASYNC-HELPER CALL
    ***************************************************************************/
    var expressResponse = asyncHelper(err, response);
    /**************************************************************************/
    res.status(expressResponse.code)
      .send(expressResponse);
  });
};

Expected arguments

Async-helper expects a boolean (err) and an array (response).

As this function is called after the execution of async, the boolean (err) should be set after all functions and be set to false if any of the functions had an error.

The array (response) is the array generated by the async execution.

In the functions executed by async, if some error occour, you should set a "code" in the response object sent to the callback.

Example:

module.exports = {
  validate: function(gitCheckout, callback) {
    var response = {};
    var paramsMissing = [];
    var err = false;

    if (!gitCheckout) {
      err = true;
      paramsMissing.push('missing GIT checkout hash');
    }

    if (err === true) {
      /*************************************************************************
      * SET THE HTML ERROR CODE IN THE 'code' element
      *************************************************************************/
      response.code = 400;
      /*************************************************************************
      * SET OPTIONAL RESPONSES
      *************************************************************************/
      response.errors = paramsMissing;
    }
    callback(err, response);
  }
};

After all the functions executions, you should get an array like this one:

[ // This array was generated in sequential functions
	{
		code: 400,
		errors: ['missing GIT checkout hash']
	},
  {},
  {},
	[ // This array was generate in parallell as the last sequential function
		{},
		{
		'en-US': {
			mobletLabel: 'Fidelity Card',
			mobletHint: 'Create a fidelity card to your customers'
		},
		'pt-BR': {
			mobletLabel: 'Cartão de Fidelidade',
			mobletHint: 'Crie um cartão de fidelidade para seus clientes'
		},
		{}
	]
]

Function return

If some error occoured, you should get an object like this:

{
	code: 405,
	error: 'Moblet already exists. Perform PUT to update'
}

In a success, something like this:

{
	code: 200,
	errors: [],
	'en-US': {
		mobletLabel: 'Fidelity Card',
		mobletHint: 'Create a fidelity card to your customers'
	},
	'pt-BR': {
		mobletLabel: 'Cartão de Fidelidade',
		mobletHint: 'Crie um cartão de fidelidade para seus clientes'
	}
}