Mike Graf
Mike Graf

Reputation: 5317

Nodejs block until async calls complete?

How would one make a nodejs application wait for async calls to finish?

main.coffee:

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

if one runs this like coffee main.coffee it will exit before doing the work.

Upvotes: 0

Views: 281

Answers (1)

Ry-
Ry-

Reputation: 225238

You’re not calling the required async.each callback.

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

Upvotes: 2

Related Questions