在nodejs中循环完成后执行条件语句
Execute conditional statement after loop finishes in nodejs
我有一个 for 循环来检查多个上传图像的宽高比,完成循环后我想在 if else 条件下检查比率以重定向用户。问题是在循环完成之前检查条件,我需要在检查条件之前完成循环。我发现 async while 可能适合这里,但我对最佳实现方法感到困惑,任何人都可以给我解决方法来顺序执行代码。
//check image ratio
var validImageRatio = true;
for(i=0; i<req.files.propertyPhoto.length; i++){
var tempFile = req.files.propertyPhoto[i].tempFilePath.replace(/\/g, '/');
var ratio;var width;var height;
var acceptedRatio = 3;
//get image ratio
sizeOf(tempFile, function (err, dimensions) {
width = dimensions.width;
height = dimensions.height;
ratio = width/height;
});
if (ratio < (acceptedRatio - 0.1) || ratio > (acceptedRatio + 0.1)) {
validImageRatio = false;
break;
}
}
//if ratio invalid, redirect
if (!validImageRatio) {
...
}
//if ratio valid, upload
else{
...
}
我猜你是什么意思,但是除了包含“break”语句外,for 循环会在检查底部条件之前完成。 break 语句使 for 循环停止执行并继续执行。
由于您正在异步执行检查,同步代码将首先 运行。如果您在 for 循环中使用 async/await,它将阻塞循环的每次迭代,从而使它 运行 变慢。您可以采用的方法是同时使用 Promise.all
到 运行 检查。
const promises = req.files.propertyPhoto.map(prop => new Promise(resolve => {
const tempFile = prop.tempFilePath.replace(/\/g, '/');
const acceptedRatio = 3;
// get image ratio
sizeOf(tempFile, function (err, dimensions) {
const width = dimensions.width;
const height = dimensions.height;
const ratio = width / height;
if (ratio < (acceptedRatio - 0.1) || ratio > (acceptedRatio + 0.1)) {
return resolve(false);
}
resolve(true);
});
}));
const result = await Promise.all(promises);
if (result.some(r => r === false)) {
// if any of the ratio is invalid, redirect
} else {
// else upload
}
我有一个 for 循环来检查多个上传图像的宽高比,完成循环后我想在 if else 条件下检查比率以重定向用户。问题是在循环完成之前检查条件,我需要在检查条件之前完成循环。我发现 async while 可能适合这里,但我对最佳实现方法感到困惑,任何人都可以给我解决方法来顺序执行代码。
//check image ratio
var validImageRatio = true;
for(i=0; i<req.files.propertyPhoto.length; i++){
var tempFile = req.files.propertyPhoto[i].tempFilePath.replace(/\/g, '/');
var ratio;var width;var height;
var acceptedRatio = 3;
//get image ratio
sizeOf(tempFile, function (err, dimensions) {
width = dimensions.width;
height = dimensions.height;
ratio = width/height;
});
if (ratio < (acceptedRatio - 0.1) || ratio > (acceptedRatio + 0.1)) {
validImageRatio = false;
break;
}
}
//if ratio invalid, redirect
if (!validImageRatio) {
...
}
//if ratio valid, upload
else{
...
}
我猜你是什么意思,但是除了包含“break”语句外,for 循环会在检查底部条件之前完成。 break 语句使 for 循环停止执行并继续执行。
由于您正在异步执行检查,同步代码将首先 运行。如果您在 for 循环中使用 async/await,它将阻塞循环的每次迭代,从而使它 运行 变慢。您可以采用的方法是同时使用 Promise.all
到 运行 检查。
const promises = req.files.propertyPhoto.map(prop => new Promise(resolve => {
const tempFile = prop.tempFilePath.replace(/\/g, '/');
const acceptedRatio = 3;
// get image ratio
sizeOf(tempFile, function (err, dimensions) {
const width = dimensions.width;
const height = dimensions.height;
const ratio = width / height;
if (ratio < (acceptedRatio - 0.1) || ratio > (acceptedRatio + 0.1)) {
return resolve(false);
}
resolve(true);
});
}));
const result = await Promise.all(promises);
if (result.some(r => r === false)) {
// if any of the ratio is invalid, redirect
} else {
// else upload
}