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-if-else

v1.1.1

Published

'conditional if to async control flow'

Downloads

1,375

Readme

async-if-else

This module adds if else conditional capabilities to async module.

Ever came across code like this on your async.waterfall flow?

var async = require('async');

async.waterfall([
  async.constant({email: '[email protected]', dogs: 2, money: 0, fun: 100 }),
  updateUserEmailOrGetLegacy,
  sendEmail
], handler);

function updateUserEmailOrGetLegacy(user, cb) {
  emailExists(user.email, function (err, value) {
    if (err)
      callback(err)
    else {
      if (user.email)
        updateAccount(user, cb);
      else
        importFromLegacyByEmail(user, cb)
    }
  })
}

Using async-if-else you can have a conditional waterfall without the need of a wrapper function.
And the code is so much more readable, don't you agree?

var async = require('async-if-else')(require('async'));

function emailExists(user, callback) {
  user.find(user.email, function(err, dbUser){
    if (err)
      return callback(error);
      
     if(!dbUser)
       return callback(null, false); // does not exist, predicate will be false
       
     callback(null, true);  
  });
}

function updateAccount(user, callback) { 
  user.update( ..., callback);
}

function importFromLegacyByEmail(user, callback) { 
  remoteClient.get(user, callback);
}

async.waterfall([
  async.constant({email: '[email protected]', dogs: 2, money: 0, fun: 100 }),
  async.if(emailExists, updateAccount).else(importFromLegacyByEmail),
  sendEmail
], handler);

You can also omit the else and the function is only executed if the predicate is true.

var async = require('async-if-else')(require('async'));

async.waterfall([
  async.constant({email: '[email protected]', dogs: 2, money: 0, fun: 100 }),
  async.if(emailExists, auditLogging),
  publishToQueue
], handler);

if you don't want to change the async object, you can always do something like that

var async = require('async');
var conditional = require('async-if-else')({});

async.waterfall([
  async.constant({email: '[email protected]', dogs: 2, money: 0, fun: 100 }),
  conditional.if(emailExists, auditLogging),
  publishToQueue
], handler);

API

async.if()
async.if().else()
async.ifNot()
async.ifNot().else()
async.unless() 
async.unless().else()
 var conditionals = require('async-if-else')({});
 var async = require('async-if-else')(require('async'));
async.if(predicateFn, expressionFn)
  • predicateFn must validate the argument received from waterfall chain
  function predicateFn(arg1 [, arg2 ...], callback) {}

callback signature is callback(error, truthyValue|falsyValue);
if you pass an error on first parameter the async.waterfall will skip below steps and goes directly to async.waterfall's callback function.

  • expressionFn will execute when predicate is thruthy.
  function expressionFn(arg1 [,arg2...], asyncWaterfallCallback)
async.if(predicateFn, expressionFn).else(elseExpressionFn);
  • elseExpressionFn is executed when the if predicate is falsy or ifNot predicate is truthy.
	function elseExpressionFn(arg1 [,arg2...], asyncWaterfallCallback)

ifNot works as the opposite of If receives exactly the same arguments

async.ifNot(predicateFn, expressionFn)

you could use with else as well

async.ifNot(predicateFn, expressionFn).else(elseExpressionFn)

Hey, did you found an issue?

The best way to get in touch is using the GitHub issues section.
If you can't find someone with the problem you are facing open a new issue and let me know.
If you manage to find a solution for your problem, you can submit a new PR :)

Let's make the world a better place by helping others.

License

MIT