处理从异步函数中获取的数据
Work with data that I get out of an async function
我正在进行异步网络搜索,查找本地网络中所有设备的 IP 地址。
现在我想从找到的设备中找到一个与 mac 地址匹配的特定 IP。
我的问题是,由于搜索是异步的,我不能只获取输出,将其作为数组保存到 const 中,然后在其中搜索特定的 mac 地址。它总是给我 "promise undefined" 输出。
为了清楚起见,这是我的代码:
const find = require('local-devices');
async function findIP() {
// Find all local network devices.
const found = find().then(devices => {
console.log(devices);
})
await found;
// here I want to work with the data thats saved inside found and find the ip adress
console.log(found);
}
findIP();
这个输出是:
[
{ name: '?', ip: '192.168.0.43', mac: '2a:f2:8c:83:26:8a' },
{ name: '?', ip: '192.168.0.91', mac: '98:da:c4:ff:1c:e1' },
{ name: '?', ip: '192.168.0.152', mac: 'dc:a6:32:01:aa:cc' },
{ name: '?', ip: '192.168.0.175', mac: '00:17:88:65:f4:2d' },
{ name: '?', ip: '192.168.0.182', mac: '4e:98:8d:e5:05:51' },
{ name: '?', ip: '192.168.0.211', mac: '80:be:05:73:bc:g5' }
]
Promise { undefined }
所以,我实际上想用 "found" 数组做的是:
const element = found.find(d => d.mac === '2a:f2:8c:83:26:8a');
console.log(element ? element.ip : '');
这样我就可以得到一个特定 mac 地址的 IP。但是如果我把它放在我的代码中,(而不是 console.log(found)
,我会得到一个“UnhandledPromiseRejectionWarning: TypeError: found.find is not a function
”
所以我必须把它放在哪里?我发现了一些像这样等待特定时间的函数:
function resolveAfter2Seconds() {
return new Promise(resolve => {
setTimeout(() => {
resolve('resolved');
}, 2000);
});
}
async function asyncCall() {
console.log('calling');
const result = await resolveAfter2Seconds();
console.log(result);
// expected output: 'resolved'
}
asyncCall();
但我不知道将此函数放在我的代码中的什么位置,以便语法正确并且等待足够长的时间,从而使 promise 不再挂起。
所以基本上我只想告诉我的程序:开始网络搜索,等待 5 秒,然后将所有找到的设备保存在一个 const xy 中。之后做 console.log(xy)
我想做的事情有可能吗?
您需要 return 您的承诺中的某些内容,并且您需要在某处分配该 returned 值。
const find = require('local-devices');
async function findIP() {
// Find all local network devices.
const found = find().then(devices => {
return devices;
});
// you could skip the .then(...) in the above example
// if you only return what is passed to the .then
const foundDevices = await found;
// here I want to work with the data thats saved inside found and find the ip adress
console.log(foundDevices);
}
findIP();
根据您的具体要求,您可以完全跳过 .then
,只跳过 const found = await find();
const find = require('local-devices');
async function findIP() {
const found = await find();
const element = found.find(d => d.mac === '2a:f2:8c:83:26:8a');
console.log(element ? element.ip : '');
}
findIP();
我正在进行异步网络搜索,查找本地网络中所有设备的 IP 地址。
现在我想从找到的设备中找到一个与 mac 地址匹配的特定 IP。
我的问题是,由于搜索是异步的,我不能只获取输出,将其作为数组保存到 const 中,然后在其中搜索特定的 mac 地址。它总是给我 "promise undefined" 输出。
为了清楚起见,这是我的代码:
const find = require('local-devices');
async function findIP() {
// Find all local network devices.
const found = find().then(devices => {
console.log(devices);
})
await found;
// here I want to work with the data thats saved inside found and find the ip adress
console.log(found);
}
findIP();
这个输出是:
[
{ name: '?', ip: '192.168.0.43', mac: '2a:f2:8c:83:26:8a' },
{ name: '?', ip: '192.168.0.91', mac: '98:da:c4:ff:1c:e1' },
{ name: '?', ip: '192.168.0.152', mac: 'dc:a6:32:01:aa:cc' },
{ name: '?', ip: '192.168.0.175', mac: '00:17:88:65:f4:2d' },
{ name: '?', ip: '192.168.0.182', mac: '4e:98:8d:e5:05:51' },
{ name: '?', ip: '192.168.0.211', mac: '80:be:05:73:bc:g5' }
]
Promise { undefined }
所以,我实际上想用 "found" 数组做的是:
const element = found.find(d => d.mac === '2a:f2:8c:83:26:8a');
console.log(element ? element.ip : '');
这样我就可以得到一个特定 mac 地址的 IP。但是如果我把它放在我的代码中,(而不是 console.log(found)
,我会得到一个“UnhandledPromiseRejectionWarning: TypeError: found.find is not a function
”
所以我必须把它放在哪里?我发现了一些像这样等待特定时间的函数:
function resolveAfter2Seconds() {
return new Promise(resolve => {
setTimeout(() => {
resolve('resolved');
}, 2000);
});
}
async function asyncCall() {
console.log('calling');
const result = await resolveAfter2Seconds();
console.log(result);
// expected output: 'resolved'
}
asyncCall();
但我不知道将此函数放在我的代码中的什么位置,以便语法正确并且等待足够长的时间,从而使 promise 不再挂起。
所以基本上我只想告诉我的程序:开始网络搜索,等待 5 秒,然后将所有找到的设备保存在一个 const xy 中。之后做 console.log(xy)
我想做的事情有可能吗?
您需要 return 您的承诺中的某些内容,并且您需要在某处分配该 returned 值。
const find = require('local-devices');
async function findIP() {
// Find all local network devices.
const found = find().then(devices => {
return devices;
});
// you could skip the .then(...) in the above example
// if you only return what is passed to the .then
const foundDevices = await found;
// here I want to work with the data thats saved inside found and find the ip adress
console.log(foundDevices);
}
findIP();
根据您的具体要求,您可以完全跳过 .then
,只跳过 const found = await find();
const find = require('local-devices');
async function findIP() {
const found = await find();
const element = found.find(d => d.mac === '2a:f2:8c:83:26:8a');
console.log(element ? element.ip : '');
}
findIP();