node.jsのasyncつかう時、なかで同期的な処理してるとスタックオーバーフローする。 setTimeoutでコールバックを呼べば非同期になるから大丈夫になる
async = require "async" max = 100000 count = 0 # setTimeoutで非同期に処理させる timer = (cb) -> setTimeout -> do cb , 0 console.log "#{max} loop start" async.whilst( -> count < max (cb) -> count++ console.log "#{count} / #{max}" if count % 1000 is 0 # do cbだとrangeErrorになる timer cb -> console.log "#{count} / #{max} loop done " )