Skip to content

Instantly share code, notes, and snippets.

@tatat
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tatat/b98959006f87d0acb691 to your computer and use it in GitHub Desktop.
Save tatat/b98959006f87d0acb691 to your computer and use it in GitHub Desktop.
Array::forEachAsync = (each, context) ->
copy = @slice()
index = -1
do next = ->
return if copy.length == 0
process.nextTick ->
item = copy.shift()
index += 1
each.call context, item, index, next
[0..2].forEach (i) ->
[0..5].forEachAsync (value, index, next) ->
process.nextTick =>
console.log i, value
next()
# 0 0
# 1 0
# 2 0
# 0 1
# 1 1
# 2 1
# 0 2
# 1 2
# 2 2
# 0 3
# 1 3
# 2 3
# 0 4
# 1 4
# 2 4
# 0 5
# 1 5
# 2 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment