node-ragtime
Async functions for CoffeeScript: map, reduce, and more!
{async,delay,repeat} = require 'ragtime' # equivalent to setTimeout(function(){ console.log(42);}, 5000);delay 5000, -> console.log 42 # equivalent to delay 1, -> console.log 42async -> console.log 42 repeat 2000, -> console.log 42
There is also a "functional programming" way of doing it:
{wait} = require 'ragtime' class Foo constructor: -> @value = "42" hello: (name) => wait(4000) => console.log "hello #{name}, value is #{@value}"
It is funnier is you use node-timmy with it:
{wait} = require 'ragtime'require 'timmy' f = -> wait(4.seconds) -> console.log "hello"f()
Async Map / Reduce / Foreach
These functions provide non-blocking iterators with a damn cool syntax:
map
{reduce, repeat} = require 'ragtime' repeat 400, -> console.log "computing.." # fully non-blocking and async reducebar = map [0..80000], (x) -> x * xbar (err, result) -> console.log "result 2: #{result.length} sample: #{result[44444..44454]}"
reduce
{reduce, repeat} = require 'ragtime' repeat 400, -> console.log "computing.." # fully non-blocking and async reducebar = reduce [0..80000], (x,y=0) -> x * x + ybar (err, result) -> console.log "result 2: #{result}"
foreach
{reduce, repeat} = require 'ragtime' repeat 50, -> console.log "." bar = foreach [0..500], (x,y) -> console.log xbar (err) -> console.log "terminated"
Throttling
The throttle function is like delay() but with a random trigger. You may find it useful.
{throttle, reduce, repeat, async} = require 'ragtime' repeat 400, -> console.log "computing.." # other functions-friendly reducerbar = reduce [0..60000], (x,y=0) -> throttle(0.95) -> x * x + ybar (err, result) -> console.log "result 2: #{result}" async process.exit
Changelog
- 0.0.7 - fixed a major logic flaw - I should have wrote unit tests.. fuck!!
- 0.0.0 - initial crappy commit