How to send result of a function after the loop is finished?
问题 In a Node/Express server in CoffeeScript, I have the following function : @resolveServers = (url, servers, answer) -> result = [] treatServer(url, server, (treatAnswer) -> result.push(treatAnswer) ) for server in servers answer(result) The treatServer method take some times, and the answer is send before the loop is finish. How can I send the result only when the loop is come at an end ? Thanks for the help. 回答1: It is easy as long as you know the number of servers beforehand: @resolveServers