没有循环节点JS将数据插入变量
我尝试将数据插入变量中,并在节点js内部函数中循环循环,但是当我尝试将数据推入电报bot时,变量为空,如何正确地将数据插入变量。
代码:
async function brokeServer(listServer) {
let nothing = '';
for (const newServer of listServer) {
ping.sys.probe(newServer.srv_ip, async function(isAlive){
let msg = isAlive ? 'host ' + newServer.srv_ip + ' is alive' : 'host ' + newServer.srv_ip + ' is dead';
//console.log(msg);
let myspace = '\n';
nothing+=myspace;
nothing+=msg;
});
}
MeikelBot.sendMessage(-721865824, nothing);
}
i try to insert data into variable with looping on node js inside function, but when i try to push the data into telegram bot, the variable is empty, how to insert the data into the variable properly.
the code :
async function brokeServer(listServer) {
let nothing = '';
for (const newServer of listServer) {
ping.sys.probe(newServer.srv_ip, async function(isAlive){
let msg = isAlive ? 'host ' + newServer.srv_ip + ' is alive' : 'host ' + newServer.srv_ip + ' is dead';
//console.log(msg);
let myspace = '\n';
nothing+=myspace;
nothing+=msg;
});
}
MeikelBot.sendMessage(-721865824, nothing);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它的原因是
ping.sys.probe
仍然具有异步且尚未正确处理。您需要将其转换为Promise
,然后应该与async
函数一起使用&amp; <代码> 循环。示例(请注意,未经测试):
Its because the
ping.sys.probe
is still asynchronous and not yet handled properly. You need to turn it toPromise
then it should work with theasync
function &for
loop.Example (please note this is untested):