Async Await Promise.all Array.map 未按预期运行。不知道为什么
Async Await Promise.all Array.map not behaving as expected. Not Sure Why
我在我的一个项目的异步函数中有以下内容。 matchCamera
、matchIP
和 matchAudio
都是 return 布尔值或错误。
如果错误得到 returned,我希望它落入我的 master catch 中,以便我可以处理它,但这并没有发生。
try {
// .... Additional code here
const typecheck = await Promise.all(
evoCfg.cameras.map(async camera => {
if (camera.type === 'H264') {
return await matchCamera(camera);
} else if (camera.type === 'RTSP') {
return await matchIP(camera);
} else if (camera.type === 'AUDIO') {
return await matchAudio(camera);
} else {
// Motion JPEG
return true;
}
})
);
// .... additional code here
console.log('results:);
console.dir(typecheck, {depth: null, colors: true});
} catch (e) {
console.error('In Master Catch:', e);
}
当我导致错误情况时,我不断得到的输出是:
results:
[ true,
true,
true,
true,
true,
true,
Error: MAC for IP Cam not found on Network: 00:11:22:33:44:55
at matchIP (/file.js:58:15)
at process._tickCallback (internal/process/next_tick.js:68:7),
true ]
我期待:
In Master Catch: MAC for IP Cam not found on Network: 00:11:22:33:44:55
at matchIP (/file.js:58:15)
at process._tickCallback (internal/process/next_tick.js:68:7)
const matchIP = async source => {
let arpScan = [];
for (const c in obj.arr){
if(obj.arr[c].name === source.source) {
try {
arpScan = await scannerP();
let arpIdx = searchObjArray(source.mac, source.mac, 'mac', 'mac', arpScan);
if(arpIdx === -1) {
// not found on network
throw new Error(`MAC for IP Cam not found on Network: ${source.mac}`);
}
for (const cs in obj.arr[c].sources) {
if (
obj.arr[c].sources[cs].id === arpScan[arpIdx].mac &&
obj.arr[c].sources[cs].url === `rtsp://${arpScan[arpIdx].ip}/${source.streamPort}`
) {
return true;
}
}
let recorderIdx = searchObjArray(
'rtsp',
source.mac,
'fmt',
'id',
obj.arr[c].sources
);
source.streamAddress = arpScan[arpIdx].ip;
obj.arr[c].sources[recorderIdx].url = `rtsp://${arpScan[arpIdx].ip}/${source.streamPort}`;
return false;
} catch (e) {
return e;
}
}
}
};
The Promise.all() method returns a single Promise that resolves when all of the promises passed as an iterable have resolved or when the iterable contains no promises. It rejects with the reason of the first promise that rejects.
你正在破坏 Promise.all
的目的。它需要一个承诺列表,但你给出了一个承诺的值列表。
const typecheckArray = await Promise.all(
evoCfg.cameras.map(camera => {
if (camera.type === 'H264') {
return matchCamera(camera);
} else if (camera.type === 'RTSP') {
return matchIP(camera);
} else if (camera.type === 'AUDIO') {
return matchAudio(camera);
} else {
// Motion JPEG
return Promise.resolve(true);
}
})
);
此外,Promise.all
returns 一组值。
问题是你的matchIP
函数里面有
try {
// ... code
catch (e) {
return e;
}
因此它将错误作为一个值返回,而不是将其抛给您的外部块来捕获。
另外,正如其他人指出的那样。您主要是通过在 map
函数中使用 await
来破坏 Promise.all
的值。把await
s拿出来。
我在我的一个项目的异步函数中有以下内容。 matchCamera
、matchIP
和 matchAudio
都是 return 布尔值或错误。
如果错误得到 returned,我希望它落入我的 master catch 中,以便我可以处理它,但这并没有发生。
try {
// .... Additional code here
const typecheck = await Promise.all(
evoCfg.cameras.map(async camera => {
if (camera.type === 'H264') {
return await matchCamera(camera);
} else if (camera.type === 'RTSP') {
return await matchIP(camera);
} else if (camera.type === 'AUDIO') {
return await matchAudio(camera);
} else {
// Motion JPEG
return true;
}
})
);
// .... additional code here
console.log('results:);
console.dir(typecheck, {depth: null, colors: true});
} catch (e) {
console.error('In Master Catch:', e);
}
当我导致错误情况时,我不断得到的输出是:
results:
[ true,
true,
true,
true,
true,
true,
Error: MAC for IP Cam not found on Network: 00:11:22:33:44:55
at matchIP (/file.js:58:15)
at process._tickCallback (internal/process/next_tick.js:68:7),
true ]
我期待:
In Master Catch: MAC for IP Cam not found on Network: 00:11:22:33:44:55
at matchIP (/file.js:58:15)
at process._tickCallback (internal/process/next_tick.js:68:7)
const matchIP = async source => {
let arpScan = [];
for (const c in obj.arr){
if(obj.arr[c].name === source.source) {
try {
arpScan = await scannerP();
let arpIdx = searchObjArray(source.mac, source.mac, 'mac', 'mac', arpScan);
if(arpIdx === -1) {
// not found on network
throw new Error(`MAC for IP Cam not found on Network: ${source.mac}`);
}
for (const cs in obj.arr[c].sources) {
if (
obj.arr[c].sources[cs].id === arpScan[arpIdx].mac &&
obj.arr[c].sources[cs].url === `rtsp://${arpScan[arpIdx].ip}/${source.streamPort}`
) {
return true;
}
}
let recorderIdx = searchObjArray(
'rtsp',
source.mac,
'fmt',
'id',
obj.arr[c].sources
);
source.streamAddress = arpScan[arpIdx].ip;
obj.arr[c].sources[recorderIdx].url = `rtsp://${arpScan[arpIdx].ip}/${source.streamPort}`;
return false;
} catch (e) {
return e;
}
}
}
};
The Promise.all() method returns a single Promise that resolves when all of the promises passed as an iterable have resolved or when the iterable contains no promises. It rejects with the reason of the first promise that rejects.
你正在破坏 Promise.all
的目的。它需要一个承诺列表,但你给出了一个承诺的值列表。
const typecheckArray = await Promise.all(
evoCfg.cameras.map(camera => {
if (camera.type === 'H264') {
return matchCamera(camera);
} else if (camera.type === 'RTSP') {
return matchIP(camera);
} else if (camera.type === 'AUDIO') {
return matchAudio(camera);
} else {
// Motion JPEG
return Promise.resolve(true);
}
})
);
此外,Promise.all
returns 一组值。
问题是你的matchIP
函数里面有
try {
// ... code
catch (e) {
return e;
}
因此它将错误作为一个值返回,而不是将其抛给您的外部块来捕获。
另外,正如其他人指出的那样。您主要是通过在 map
函数中使用 await
来破坏 Promise.all
的值。把await
s拿出来。