如何在 node.js 中停止异步执行
How to stop async execution in node.js
我正在尝试为给定的主机名动态添加 IP 地址。
代码片段
// This function will return me ip address of my server
dns.lookup('testwsserver', function (err, result {
hostIP = result;
console.log("Inside : "+hostIP); // Log 1
});
console.log("Outside : "+hostIP); // Log 2
var options = {
host : hostIP,
port : '8080',
path : null,
method : 'POST',
};
console.log(options); // Log 3
以上代码只是获取给定主机名的 IP 地址并将其分配给变量 "hostIP",问题是当在循环外显示或在选项中使用时,我在 hostIP 中获得空值。
输出 -
Outside : null // Log 2
{ hostname: null, // Log 3
port: '8080',
path: null,
method: 'POST',
}
Inside : 192.168.253.18 // Log 1
根据我的需要,代码应该按顺序执行,首先查找函数应该给hostIP赋值,然后休息执行。
感谢任何帮助!!
如您所说,node.js 是异步的,您必须执行以下操作:
// This function will return me ip address of my server
dns.lookup('testwsserver', function (err, result {
hostIP = result;
console.log("Inside : "+hostIP); // Log 1
console.log("Inside1 : "+hostIP); // Log 2
var options = {
host : hostIP,
port : '8080',
path : null,
method : 'POST',
};
console.log(options); // Log 3
});
参考下面的答案和一些修改,我得到了我的结果..
这是最后的代码片段...
dns.lookup('soawsserver', function (err, result) {
hostIP = result;
// Only exception handling, in case dns look up fails to find server
if(hostIP == null) {
console.log("\nUnable to detect server, pl check /etc/hosts file.\n")
}
else {
var options = {
host : hostIP,
port : '8080',
path : null,
method : 'POST',
};
};
PS : Is there any better way to solve this problem, like instead of
putting my whole code in a look up method can i solve this problem
sequentially?
Like first running look up method then initializing my
request.
Any help is appreciated!!
Thanks
我正在尝试为给定的主机名动态添加 IP 地址。
代码片段
// This function will return me ip address of my server
dns.lookup('testwsserver', function (err, result {
hostIP = result;
console.log("Inside : "+hostIP); // Log 1
});
console.log("Outside : "+hostIP); // Log 2
var options = {
host : hostIP,
port : '8080',
path : null,
method : 'POST',
};
console.log(options); // Log 3
以上代码只是获取给定主机名的 IP 地址并将其分配给变量 "hostIP",问题是当在循环外显示或在选项中使用时,我在 hostIP 中获得空值。
输出 -
Outside : null // Log 2
{ hostname: null, // Log 3
port: '8080',
path: null,
method: 'POST',
}
Inside : 192.168.253.18 // Log 1
根据我的需要,代码应该按顺序执行,首先查找函数应该给hostIP赋值,然后休息执行。
感谢任何帮助!!
如您所说,node.js 是异步的,您必须执行以下操作:
// This function will return me ip address of my server
dns.lookup('testwsserver', function (err, result {
hostIP = result;
console.log("Inside : "+hostIP); // Log 1
console.log("Inside1 : "+hostIP); // Log 2
var options = {
host : hostIP,
port : '8080',
path : null,
method : 'POST',
};
console.log(options); // Log 3
});
参考下面的答案和一些修改,我得到了我的结果.. 这是最后的代码片段...
dns.lookup('soawsserver', function (err, result) {
hostIP = result;
// Only exception handling, in case dns look up fails to find server
if(hostIP == null) {
console.log("\nUnable to detect server, pl check /etc/hosts file.\n")
}
else {
var options = {
host : hostIP,
port : '8080',
path : null,
method : 'POST',
};
};
PS : Is there any better way to solve this problem, like instead of putting my whole code in a look up method can i solve this problem sequentially?
Like first running look up method then initializing my request.
Any help is appreciated!!
Thanks