我知道我要发布这个,然后得到它的工作,更不用说标题,但...使用async.js在循环中执行异步操作
我处于我需要的情况对我给出的数组中的每个元素执行一些异步代码,然后在所有元素完成后进行一些检查。通常情况下,如果我在这里使用async.js,但是我所做的事情并没有得到我想要的结果。
所以这是我的逻辑,我想我可以创建一个函数数组,但我做的方式,这似乎有async.js返回我的功能表中的结果。
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)
我90%肯定的问题是这阵我创造不被分配了类似的我想是的,但我不知道。任何人都知道我在这里做错了什么?
对不起,这么长时间,我用打'做()'但那一点都不不工作。结束了,这是我向该阵列推进功能的方式,当时我正在使用与上述不同的东西。结束这是问题! –