0

I am new to nodejs and javascript. I am learning async programming. I am trying to find ip address for a domain name. I am using nodejs inbuild "DNS" library.

dns.resolve(domainName, function onLookup(err, addresses, family) {
            if (err){
                res.send(err);
            } else {
                 console.log(addresses);
                 var domainAddress = addresses;
            }
        });

This is the actual code. I am trying to get the domainAddress out of the call back. Since its async programming, it takes time to get the domainAddress and i am unable to access it for some time.

If i am using a code something like this, i am able to access the domainAddress

dns.resolve(domainName, function onLookup(err, addresses, family) {
                                if (err){
                                    res.send(err);
                                } else {
                                    domainAddress = addresses;
                                }
                            });

        setTimeout(function() {
          console.log(domainAddress);
        }, 1000);

But this i do not feel this is the right way to export a result from callback to the main program.

Can you please give any alternate solution for this problem ?

NOTE: I want to do additional task once i get the IP. I want to find the geo location based on the ip address. For that i need to pass this IP to a separate module called Geoip-lite. But since i am unable to get the address outside its getting very difficult

Bala
  • 381
  • 3
  • 16
  • 1
    You can't all the operations that depends on `domainAddress` should be placed within the callback – Arun P Johny Aug 26 '15 at 09:09
  • Don't use setTimeout, you don't know how many time is necessary... – Thierry Aug 26 '15 at 09:10
  • Wrap you dns.resolve call in a function with a callback as parameter. Call that callback with the domainAdress – cviejo Aug 26 '15 at 09:11
  • just look inside a function that uses callbacks and you might understand the concept more clearly – Max Bumaye Aug 26 '15 at 09:13
  • @CViejo - can you please provide an example. – Bala Aug 26 '15 at 10:28
  • @Bala take a look at BlackMamba's answer below. It's doing exactly that – cviejo Aug 26 '15 at 10:32
  • I want to do additional task once i get the IP. I want to find the geo location based on the ip address. For that i need to pass this IP to a separate module called Geoip-lite. But since i am unable to get the address outside its getting very difficult – Bala Aug 26 '15 at 10:45

2 Answers2

2
function myResolve(domainName, callback){
    dns.resolve(domainName, function onLookup(err, addresses, family) {
            if (err){
                return callback(err);
            } else {
                return callback(null, addresses);
            }
        });
}


myResolve(domainName, function(err, addresses){
    if(err){
        return res.send(err);
    } else {
        return res.send(addresses);
    }
});

Please try this code.

BlackMamba
  • 10,054
  • 7
  • 44
  • 67
2

try node.js async module. you can use async series for this.

 var domainAddress;
function findDNS(callback){
   dns.resolve(domainName, function onLookup(err, addresses, family) {
        if (err){
            res.send(err);
        } else {
             console.log(addresses);
              domainAddress = addresses;
              callback(null,"Address found")    
        }
    });
}
function doWhatever(callback){
   console.log("DNS:"+domainAddress);
   callback(null,"OK");
}
async.series([findDNS,doWhatever],function (err, results){
    console.log(results);

});
Subham
  • 1,414
  • 4
  • 17
  • 34