Skip to content

Instantly share code, notes, and snippets.

@e-jigsaw
Forked from lotz84/contEach.js
Last active August 29, 2015 14:13
Show Gist options
  • Save e-jigsaw/ca6147022ac25ad6bc7b to your computer and use it in GitHub Desktop.
Save e-jigsaw/ca6147022ac25ad6bc7b to your computer and use it in GitHub Desktop.
contMap = (arr, f, g, ans) ->
if ans is undefined then ans = []
if arr.length is 0 and typeof g is 'function' then return g ans else return
f.apply @, [
arr.shift()
(item)-> contMap arr, f, g, ans.concat([item])
ans
]
# Example
contMap queries, (query, next) ->
$.get "https://www.google.co.jp/search?q=" + query, (body) ->
# some process with body
next body
, (results) ->
@e-jigsaw
Copy link
Author

ex:

contMap = (arr, f, g, ans) ->
  if ans is undefined then ans = []
  if arr.length is 0 then return g ans
  f.apply @, [
    arr.shift()
    (item)-> contMap arr, f, g, ans.concat([item])
    ans
  ]

contMap.apply @, [
  [1, 2, 3]
  (num, callback)->
    setTimeout ->
      callback num * num
    , 20
  (ans)-> console.log ans
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment