I know I'm going to post this and then get it working, not to mention the title but...
I'm in a situation where I need to do some async code to each element in an array that I'm given, and then do some checks after all of them have finished. Normally, if I would use async.js here, however the way I'm doing it isn't getting my the results I want.
So this is my logic, I figure I can create an array of functions, however the way I'm doing this seems to have async.js returning me a list of functions in the result.
findLeadThatCanBeTaken : (leads, user, manualTake, cb) =>
if(leads.length == 0) then return cb(null, null)
funcArr = []
self = this
for lead in leads
fun = (callback) ->
console.log(lead.state)
State.get lead.state, (e, state) ->
if !state or state.canBeCalled(self.currentTime())
return callback(null, lead._id.toString)
return callback(null, null)
funcArr.push(fun)
async.parallel funcArr, (e, r) ->
if (e)
return cb(message:'No leads can be called at the current time', null)
id = _.compact(r)
id = r[0] if r.length
if (!id || !id.length)
return cb(message:'No leads can be called at the current time', null)
lead = _.filter leads, (l) -> return l._id.toString() == id
# Code handling
@takeLead(lead, user, cb)
I'm 90% sure the issue is this array I am creating isn't being assigned like I think it is, but I'm not sure. Anyone know what I'm doing wrong here?