ragtime

Async functions for CoffeeScript: map, reduce, and more

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 42
async -> 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()
 

These functions provide non-blocking iterators with a damn cool syntax:

{reduce, repeat} = require 'ragtime'
 
repeat 400, -> console.log "computing.."
 
# fully non-blocking and async reduce
bar = map [0..80000], (x) -> x * x
bar (err, result) -> 
  console.log "result 2: #{result.length} sample: #{result[44444..44454]}"
{reduce, repeat} = require 'ragtime'
 
repeat 400, -> console.log "computing.."
 
# fully non-blocking and async reduce
bar = reduce [0..80000], (x,y=0) -> x * x + y
bar (err, result) -> 
  console.log "result 2: #{result}"
{reduce, repeat} = require 'ragtime'
 
repeat 50, -> console.log "."
 
bar = foreach [0..500], (x,y) -> console.log x
bar (err) -> 
  console.log "terminated"

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 reducer
bar = reduce [0..60000], (x,y=0) -> throttle(0.95) -> x * x + y
bar (err, result) -> 
  console.log "result 2: #{result}"
  async process.exit
 
  • 0.0.7 - fixed a major logic flaw - I should have wrote unit tests.. fuck!!
  • 0.0.0 - initial crappy commit