Git Product home page Git Product logo

sleep-async's Introduction

Sleep-Async Node.js CI

Non blocking asynchronous sleep, with watching condition.

The library have two version:

  • Classic - standard methods with callback function
    • How to initialize object? e.g.
      const sleep = require('sleep-async')();
      
  • Promise - the methods returns Promise object
    • How to initialize object? e.g.
      const sleep = require('sleep-async')().Promise;
      

Classic library has methods:

  • sleep(timeout, done)

    • timeout - sleep time in milisecond,
    • done - callback runned always after sleep.
  • sleepWithCondition(condition, timeout, done)

    • condition - condition function has checked on any sleep cycle. When condition is true, the sleep is done.
    • timeout - max timeout to sleep.
    • done - callback runned always after sleep.
  • sleepWithCondition(condition, options, done)

    • condition - condition function has checked on any sleep cycle. When condition is true, the sleep is done.
    • options - advanced options for sleep.
      • full options example:
          const options = {
            sleep: 1000,
            interval: 10
          };
    • done - callback runned always after sleep.

Quick exaples

  • Required

    const sleep = require('sleep-async')();
  • With sleep(timeout, done)

    sleep.sleep(5000, function(){
      stopTime = new Date().getTime();
      console.log('Difference: '+((stopTime-startTime)/1000)+' [s]');
    });
  • With sleepWithCondition(condition, timeout, done)

    sleep.sleepWithCondition(function(){
        return collection.length >= 10;
      },
      5000,
      function(){
        stopTime = new Date().getTime();
        console.log('Difference: '+((stopTime-startTime)/1000)+' [s]');
    });
  • With sleepWithCondition(condition, options, done)

    const options = {
      sleep: 5000,
      interval: 2500
    };
    
    sleep.sleepWithCondition(function(){
        return collection.length >= 10;
      },
      options,
      function(){
        stopTime = new Date().getTime();
        console.log('Difference: '+((stopTime-startTime)/1000)+' [s]');
      });

Promise library has methods:

  • sleep(timeout) : Promise

    • timeout - sleep time in milisecond
  • sleepWithCondition(condition, timeout) : Promise

    • condition - condition function has checked on any sleep cycle. When condition is true, the sleep is done.
    • timeout - max timeout to sleep.
  • sleepWithCondition(condition, options) : Promise

    • condition - condition function has checked on any sleep cycle. When condition is true, the sleep is done.
    • options - advanced options for sleep.
      • full options example:
          const options = {
            sleep: 1000,
            interval: 10
          };

Quick exaples

  • Required

    const sleep = require('sleep-async')().Promise;
  • With sleep(timeout)

    const startTime = new Date().getTime();
    
    sleep.sleep(5000)
      .then(() => new Date().getTime())
      .then(stopTime => console.log('Difference: '+((stopTime-startTime)/1000)+' [s]'));
  • With sleepWithCondition(condition, timeout)

    const startTime = new Date().getTime();
    
    sleep.sleepWithCondition(function(){
        return collection.length >= 10;
      },
      5000
    )
      .then(() => new Date().getTime())
      .then(stopTime => console.log('Difference: '+((stopTime-startTime)/1000)+' [s]'));
  • With sleepWithCondition(condition, options)

    const options = {
      sleep: 5000,
      interval: 2500
    };
    
    const startTime = new Date().getTime();
    
    sleep.sleepWithCondition(function(){
        return collection.length >= 10;
      },
      options
    )
      .then(() => new Date().getTime())
      .then(stopTime => console.log('Difference: '+((stopTime-startTime)/1000)+' [s]'));

sleep-async's People

Contributors

bdjibril avatar dependabot[bot] avatar sp9usb avatar

Stargazers

 avatar  avatar  avatar

sleep-async's Issues

Expose the interval parameter please

I have a long running process whereby I run a task that will take upwards of 30 minutes to complete (uploading content to a server, and 30 minutes later it is replicated to our CDN). I would like to not poll every 10ms, but once every minute. I can code around this by exposing a time condition as well as an availability condition in the test, but this would be easier if I could specify the interval manually. It would mean my grunt process would run less CPU in the downtime.

Many thanks,

David

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.