Non blocking asynchronous sleep, with watching condition.
npm install sleep-asyncSleep-Async
===========
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;
timeout
Classic library has methods:
- sleep(timeout, done)
+ - sleep time in milisecond,
done
+ - callback runned always after sleep.
condition
- sleepWithCondition(condition, timeout, done)
+ - 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.
condition
- sleepWithCondition(condition, options, done)
+ - condition function has checked on any sleep cycle. When condition is true, the sleep is done.
options
+ - advanced options for sleep.
`
* full options example:
javascript
`
const options = {
sleep: 1000,
interval: 10
};
done
+ - callback runned always after sleep.
`
Quick exaples
- Required
javascript
`
const sleep = require('sleep-async')();
sleep(timeout, done)
- With
`
javascript
`
sleep.sleep(5000, function(){
stopTime = new Date().getTime();
console.log('Difference: '+((stopTime-startTime)/1000)+' [s]');
});
sleepWithCondition(condition, timeout, done)
- With
`
javascript
`
sleep.sleepWithCondition(function(){
return collection.length >= 10;
},
5000,
function(){
stopTime = new Date().getTime();
console.log('Difference: '+((stopTime-startTime)/1000)+' [s]');
});
sleepWithCondition(condition, options, done)
- With
`
javascript
`
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]');
});
timeout
Promise library has methods:
- sleep(timeout) : Promise
+ - sleep time in milisecond
condition
- sleepWithCondition(condition, timeout) : Promise
+ - condition function has checked on any sleep cycle. When condition is true, the sleep is done.
timeout
+ - max timeout to sleep.
condition
- sleepWithCondition(condition, options) : Promise
+ - condition function has checked on any sleep cycle. When condition is true, the sleep is done.
options
+ - advanced options for sleep.
`
* full options example:
javascript
`
const options = {
sleep: 1000,
interval: 10
};
`
Quick exaples
- Required
javascript
`
const sleep = require('sleep-async')().Promise;
sleep(timeout)
- With
`
javascript
`
const startTime = new Date().getTime();
sleep.sleep(5000)
.then(() => new Date().getTime())
.then(stopTime => console.log('Difference: '+((stopTime-startTime)/1000)+' [s]'));
sleepWithCondition(condition, timeout)
- With
`
javascript
`
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]'));
sleepWithCondition(condition, options)
- With
`
javascript
``
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]'));