Giter VIP home page Giter VIP logo

busywait.js's Introduction

Build Status Coverage Status bitHound Overall Score Known Vulnerabilities dependencies Status devDependencies Status npm HitCount

https://nodei.co/npm/busywait.png?downloads=true&downloadRank=true&stars=true

busywait.js

Simple Async busy wait module for Node.JS

Installation

This module is installed via npm:

npm install --save busywait

Usage

Running:

const busywait = require('../lib/index').sync;

const waitUntil = Date.now() + 2500;

function syncCheck(iteration) {
    console.log('running iteration', iteration);
    return Date.now() > waitUntil;
}

busywait(syncCheck, {
    sleepTime: 500,
    maxChecks: 20
})
    .then(function (result) {
        console.log('finished after', result.iterations, 'iterations', 'with' +
            ' result', result.result);
    });

or:

const busywait = require('../lib/index').async;

const waitUntil = Date.now() + 2500;

function asyncCheck(iteration) {
    return new Promise(function (resolve, reject) {
        console.log('running iteration', iteration);
        if (Date.now() > waitUntil) {
            return resolve(true);
        } else {
            return reject();
        }
    });
}

busywait(asyncCheck, {
    sleepTime: 500,
    maxChecks: 20
})
    .then(function (result) {
        console.log('finished after', result.iterations, 'iterations', 'with' +
            ' result', result.result);
    });

Will result in:

running iteration 1
running iteration 2
running iteration 3
running iteration 4
running iteration 5
running iteration 6
finished after 6 iterations with result true

Methods

sync(syncCheckFn, options): Promise

The syncCheckFn first argument is the function to run on each iteration. syncCheckFn must be a function with a boolean return value. The current iteration number will be passed as first argument to every call of syncCheckFn.

Options

mandatory
  • sleepTime - Time in ms to wait between checks
  • maxChecks - The max number of checks to perform before failing
optional
  • waitFirst - Should we wait the sleepTime before performing the first check (default: false)
  • failMsg - Custom error message to reject the promise with

Return value

Return value is a promise.

  • The promise will be resolved if the syncCheckFn returned true within a legal number of checks.
  • The promise will be rejected if the syncCheckFn rejected maxChecks times.

Promise resolved value:

  • iterations - The number of iterations it took to finish
  • result - Constant true

async(asyncCheckFn, options): Promise

The asyncCheckFn first argument is the function to run on each iteration. syncCheckFn must be a function with a promise return value. The current iteration number will be passed as first argument to every call of asyncCheckFn.

Options

mandatory
  • sleepTime - Time in ms to wait between checks
  • maxChecks - The max number of checks to perform before failing
optional
  • waitFirst - Should we wait the sleepTime before performing the first check (default: false)
  • failMsg - Custom error message to reject the promise with

Return value

Return value is a promise.

  • The promise will be resolved if the asyncCheckFn was resolved within a legal number of checks.
  • The promise will be rejected if the asyncCheckFn rejected maxChecks times.

Promise resolved value:

  • iterations - The number of iterations it took to finish
  • result - The resolved value of asyncCheckFn

Contributing

Please make all pull requests to the master branch and ensure tests pass locally.

busywait.js's People

Contributors

regevbr avatar

Watchers

James Cloos avatar Liel Chayoun avatar

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.