如何将 dns.lookup 的返回值存储在变量中?如果我将它存储在一个数组中,它会显示为空白。
以下是我正在处理的代码:
const dns = require('dns');
class Network
{
search(host)
{
let options = {
hints: dns.ADDRCONFIG | dns.V4MAPPED,
all: true,
verbatim: true
}
let addr = [];
dns.lookup(host, options, (error, address) =>
{
if(error)
{
console.log("Could not resolve %s\n", host);
console.log(error);
}
address.forEach(ip => {
addr.push({
address: ip.address,
family: ip.family
});
});
});
console.log(addr);
return addr;
}
}
const network = new Network();
let output = network.search("www.google.com");
console.log(output);
代码的输出为空白即 []
请提出补救措施。
相关分类