Skip to content

Instantly share code, notes, and snippets.

@wizardwerdna
Last active December 15, 2015 21:49
Show Gist options
  • Save wizardwerdna/5328792 to your computer and use it in GitHub Desktop.
Save wizardwerdna/5328792 to your computer and use it in GitHub Desktop.
A plugin substitute for nextTick that avoids the "recursive nextTick" bug. After enqueue = require('./enqueue').enqueue, simply replace every call to nextTick in your program that might begin a recursive call, and replace "process.nextTick" with enqueue.
root = (exports ? this)
nextTick = process.nextTick
root.enqueue = do(queue=undefined)->
trampoline = ->
while next = queue.shift()
next()
queue = undefined
(f) ->
if queue?
queue.push f
else
queue = [f]
nextTick -> trampoline()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment