【问题标题】:Nodejs block until async calls complete?Nodejs阻塞直到异步调用完成?
【发布时间】:2016-05-31 04:10:55
【问题描述】:

如何让 nodejs 应用程序等待异步调用完成?

主咖啡:

hosts = require './suhosts'
dns = require 'dns'
async = require 'async'

data = {}
async.each hosts, (host, cb_e) ->
   dns.lookup host,  (err, address, family)-> data[host] = address
 , () ->
   console.log data

如果有人像coffee main.coffee 这样运行它,它将在工作之前退出。

【问题讨论】:

标签: node.js coffeescript async.js


【解决方案1】:

您没有调用所需的async.each 回调。

data = {}
async.each hosts, (host, cb_e) ->
   dns.lookup host, (err, address, family) ->
      data[host] = address
      cb_e(err)
 , () ->
   console.log data

【讨论】:

    猜你喜欢
    • 2010-09-20
    • 1970-01-01
    • 2021-07-30
    • 2010-12-04
    • 1970-01-01
    • 2015-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多