mainiak
4/16/2013 - 1:59 PM

testSetImmediate.coffee

#!/usr/bin/env coffee

MAX_CALLS = 10

class Test
  constructor: ->
    @reset()
  run1: (returnFn) ->
    @count += 1
    console.log 'A| count:', @count
    callBack = => @run1 returnFn
    if @count is MAX_CALLS
      console.log returnFn
      returnFn() if returnFn?
    else
      setImmediate callBack, returnFn
  reset: ->
    @count = 0
    console.log '// reset - done'
  run2: ->
    @count += 1
    console.log 'B| count:', @count
    setImmediate @run2.bind(@) if @count isnt MAX_CALLS

t = new Test
t.run1 ->
  t.reset()
  t.run2()