Git Product home page Git Product logo

async-short-circuit's Introduction

Async Short Circuit

Performs logical AND and OR operations with short circuit on promises.

Given an array of promises, as soon as a promise whose value can cause a short circuit is resolved, it's value is used and returned and the rest of the promises are ignored.

Install

npm i async-short-circuit

Usage

const { asyncAnd, asyncOr } = require('async-short-circuit');

// or using import
import { asyncAnd, asyncOr } from 'async-short-circuit';

// Some code ...

// AND
asyncAnd(myPromises).then(value => { /* perform some logic with AND result */ });

// OR
asyncOr(myPromises).then(value => { /* perform some logic with OR result */ });

API

  • asyncAnd(promises): Promise<any> - Returned value depends on the resolve value of the given promises
    • promises: an array of Promises.
  • asyncOr(promises): Promise<any> - Returned value depends on the resolve value of the given promises
    • promises: an array of Promises.

Important Notes

Short Circuit Logic

The given promises are awaited upon, and as soon as one of them resolves, it's value is checked, if its value can cause a short circuit, the returned promise -from short-circuit functions- resolves immediately with that value without waiting for the results of the remaining promises. If the value of the resolved promise does not cause a short circuit, it's ignored and the same logic is applies to other promises.

If all none of the encountered value would cause short circuit, then the result of ANDing or ORing all the values is returned, which is the last value (the resolved value of the last promise in the given array) according to JS implementation.

Also note that any short-circuiting does not affect the running of the given promises, all the promises will always run no matter the result, it's just we don't wait for the resolving of the remaining promises if a value is received that causes a short-circuit.

Features Suggestions and Bug Reports

For feature suggestions and/or bug reports, please open an issue.

async-short-circuit's People

Contributors

amrsaber avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

async-short-circuit's Issues

Add tests

Currently, testing is run manually, using npm link.

Need to add automated tests, but -for now- I don't know exactly know how to test the short-circuiting behavior.

Might also add basic tests to make sure that the basic usage of the library is working.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.