开发者

Can't get out of exec. Local variable to pass ip address?

Question linked to Get local IP address in node.js

May I ask for a little help?

In this example of usage,

getNetworkIP(function (error, ip) {
console.log(ip);
if (error) {
 开发者_StackOverflow中文版   console.log('error:', error);
}}, false);

how is it possible to get the ip value outside the callback? For instance to use it for a connection or whatever. I tried to return it, export it, pass it, but non of these methods are working. Please help me, I'm going mental!!

Thanks!

P.S.: this code has been really useful to me, to understand a little bit more. I'm really newbie. Thanks a million for sharing it.


As Juan Mendes tried to tell you the call you are making is async. Which means that node will continue executing code and then get the callback. Whatever you need to do with your IP you can do it either inside your callback or you can save it as a global variable.

var myIP = '';

getNetworkIP(function (error, ip) {
    if (error) {
        console.log('error:', error);
    }else{
        myIP = ip;
        MyFunctionToConnectTo(myIP);
    }
}, false);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜