从作为函数参数的函数中获取值

Getting value from a function which is a parameter for a function

本文关键字:函数 获取 参数      更新时间:2023-09-26

我想知道是否有一种方法可以让我打印出这样的值?

socket.emit('location', "Hostel");
socket.on('resultOfLocation', function(msg){
    console.log(msg);   //This can print out value successfully
});
console.log(msg);  //Result is undefined. How can print out value at here?

没有解决方法,因为socket.on 的回调函数将在脚本的其余部分运行后异步调用。 因此,结果未定义的行将在回调中的任何代码之前运行。