只要您事先知道服务器的数量,这很容易:
@resolveServers = (url, servers, answer) ->
result = []
count = 0
treatServer(url, server, (treatAnswer) ->
result.push(treatAnswer)
count++
answer result if count is servers.length
) for server in servers
编辑:再次考虑之后。在您的情况下,这可以更容易地解决:
@resolveServers = (url, servers, answer) ->
result = []
treatServer(url, server, (treatAnswer) ->
result.push treatAnswer
answer result if result.length is servers.length
) for server in servers
编辑 2:正如斜眼所指出的, push 返回数组的长度:
@resolveServers = (url, servers, answer) ->
result = []
treatServer(url, server, (treatAnswer) ->
answer result if result.push(treatAnswer) is servers.length
) for server in servers
如果您想保持代码的可读性,最后一次编辑可能不是最佳选择;-)