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