
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
async-if-else
Advanced tools
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: 'thiago@email.com', 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: 'thiago@email.com', 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: 'thiago@email.com', 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: 'thiago@email.com', dogs: 2, money: 0, fun: 100 }),
conditional.if(emailExists, auditLogging),
publishToQueue
], handler);
async.if()
async.if().else()
async.ifNot()
async.ifNot().else()
async.unless()
async.unless().else()
### initializer
To get start with async-if-else you need to provide an object.
var conditionals = require('async-if-else')({});
var async = require('async-if-else')(require('async'));
### if
**if** function receives the predicate and the expression.
async.if(predicateFn, expressionFn)
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.
function expressionFn(arg1 [,arg2...], asyncWaterfallCallback)
### else
**else** receives only an elseExpressionFn
async.if(predicateFn, expressionFn).else(elseExpressionFn);
function elseExpressionFn(arg1 [,arg2...], asyncWaterfallCallback)
### ifNot
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)
### unless
just an alias to [`ifNot`](#ifNot).
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.
FAQs
'conditional if to async control flow'
We found that async-if-else demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.