whirlwinds
v0.0.5
Published
Dependecy-less micro library for timed functions
Downloads
14
Maintainers
Readme
Whirlwinds
Micro library for timed functions based on the amazing work done by bjoerge with repeat a lustrum ago.
Warning
This is a work in progress. The API is likely to be changed.
Installation
npm install whirlwinds
Usage
Sleep
const { sleep } = require('whirlwinds')
async function doSomething(){
console.log('I need to wait 3000ms..')
await sleep(3000)
console.log('3000ms have passed!!')
}
Repeat
Basic example:
const { repeat } = require('whirlwinds')
const target = ()=> console.log('printing indefinitely')
repeat(target)
.every(200)
.go()
You can pre define a maximun time by which the repeat target function is going to get executed:
const { repeat } = require('whirlwinds')
const target = ()=> console.log('printing every 200 ms until 10000 miliseconds!')
// => synchronous way:
await repeat(target).every(200).until(10000).go()
// => no longer repeating.
// => asynchronous way:
repeat(target)
.every(200)
.until(10000)
.go()
.then(()=>{
// => no longer repeating.
})
Or perhaps you want to keep executing this function only when certain criteria is met:
const { repeat } = require('whirlwinds')
const target = ()=> console.log('printing every 200 ms until 10000 miliseconds!')
repeat(target)
.every(200)
.while(()=> true) // => returning true
.go()
If things get serious, Whirlwinds allows you to pause and resume the execution of the target function whenever you feel is more convenient:
const { repeat } = require('whirlwinds')
const { pause, resume, stop } = repeat(()=>{
console.log('printing indefinitely')
})
.every(200)
.go()
// pausing...
pause()
// ...
// resuming...
resume()
Licence
M.I.T.