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 🙏

© 2025 – Pkg Stats / Ryan Hefner

async-conditions

v0.0.4

Published

Asynchronous Conditions is for parallel check multiple datas with multiple conditions.

Downloads

2

Readme

async-conditions

Asynchronous Conditions is for parallel check multiple datas with multiple conditions.

Tutorial

Download async-conditions package to your project with npm command,

npm install async-conditions@latest

Include async-conditions into your object with require('async-conditions') function.

const Validator = require('async-conditions');

Create an empty array for your check list.

let checkList = [];

Add your checks one by one to your check list with Validator.checkCondition(<condition>, <reference object>, <message>, <by object>, <error code>) method. In the below code, we will add 3 checks for same object,

let myObject = [10, 11, 12];
checkList.push(Validator.checkCondition("typeof(reference) ===  \"object\" && reference instanceof Array", myObject, "The object is not an array!", myObject, 0x1));
checkList.push(Validator.checkCondition("reference.length > 0", myObject, "This array is empty!", myObject, 0x2));
checkList.push(Validator.checkCondition("reference[0] === 10", myObject, "This array\'s first element is not 10!", myObject[0], 0x3));

NOTE: In the above code, we used "reference.length > 0" as a String. Here, reference is pointing to our <reference object> parameter.

NOTE: In the third checkList.push(...) call, we did used myObject[0] as <by> parameter, because the error is throwed by this data.

And the end! We will now use Validator.checkConditions(<check list>) asynchronous call to parallel validate our check list.

Validator.checkConditions(checkList)
    .then(function(result) {
        if(result) {
            console.log("All checks succeeded...");
        }
    })
    .catch(function(error) {
        console.log(error);
    });

// Output: All checks succeeded...

If, any of our checks did failed, the .catch() call of Validator.checkConditions() will catch the error throwing condition and console.log() the error.

All the conditions are checked paralelly. If there was an failed check while checking all conditions, it will catched fastly before all the conditions give result. So, this is awesome to use while you are using synchronous if...else if...else or switch case expressions. If your checks has going to use a little bit time, you need to use asynchronous calls!

Documentation

Class: AsyncValidator

AsyncValidator.checkCondition(condition[, reference], message[, by, code])

Added in: 0.0.1

Type

Asynchronous Function

Parameters

  • condition type:String
  • reference type:any, default:null
  • message type:String
  • by type:any, default:null
  • code type:Number, default:-1

Returns

Promise, if resolve the conditions returns false (because of no error), if reject the condition returns Error object

Usage

let myString = "Hello, World!";
AsyncValidator.checkCondition('reference.includes("Hello")', myString, "The string doesn't includes \"Hello\"!", myString, 0x90)
    .then(function(failed) {
        if(!failed) {
            console.log("The string includes \"Hello\".");
        }
    })
    .catch(function(error) {
        console.log(error);
    });

// Output: The string includes "Hello"

If, the condition did fail,

AsyncValidator.checkCondition("5 < 3", null, "Five is not smaller than three")
    .then(function(failed) {
        if(!failed) {
            console.log("Five is smaller than three.);
        }
    })
    .catch(function(error) {
        console.log(error);
    });

/*
 * Output:
 * { Error: Five is not smaller than three!
 *     at Function.generateErrorObject (<location>:<line>:<column>)
 *     at Promise (<location>)
 *     .
 *     .
 *     .
 *     at Function.Module._load (<location>:<line>:<column>) by: null, code: -1 }
 */

AsyncValidator.generateErrorObject(message[, by, code])

Added in: 0.0.1

Type

Function

Parameters

  • message type:String
  • by type:any, default:null
  • code type:Number, default:-1

Returns

Error Object with properties by and code

Usage

let myCustomErrorObject = AsyncValidator.generateErrorObject("The array doesn't have three elements.", [1, 2], 390);
console.log(myCustomErrorObject);

/*
 * Output:
 * { Error: The array doesn't have three elements.
 *     at Function.generateErrorObject (<location>:<line>:<column>)
 *     at Promise (<location>)
 *     .
 *     .
 *     .
 *     at Function.Module._load (<location>:<line>:<column>) by: [1, 2], code: 390 }
 */

Creates a custom error.

AsyncValidator.validateResults(checks)

Added in: 0.0.1

Type

Asynchronous Function

Parameters

  • checks type:Object

Returns

Promise, if resolve checks returns true (because, all conditions succeed), if reject, returns Error object

Usage

let myObject = {
    name: "John",
    lastname: "DOE",
    age: 44
};
let checkList = [
    AsyncValidator.checkCondition("typeof(reference.name) === 'string' && typeof(reference.lastname) === 'string' && typeof(reference.age) === 'number'", myObject, "name, lastname and age properties are not valid.", myObject, 0x00)
    AsyncValidator.checkCondition("reference.age => 0", myObject, "age property is not valid.", myObject.age, 0x01)
]
AsyncValidator.validateChecks(checkList)
    .then(function(success) {
        if(success) {
            console.log("Conditions are fit!");
        }
    })
    .catch(function(error) {
        console.log(error);
    });

// Output: Conditions are fit!

Checks all AsyncValidator.checkCondition() in an array asynchronously. All checks starts at the same time, if any of conditions return Error object, the process fails and shows the Error object.

If you got any problems, tell us!