Code without any handling:
for (i=0; i<dbImgCount; i++){
(function(i) {
imgDownload(FolderPath[i].FolderPath, function (next){
var url = FolderPath[i].FolderPath;
const img2 = cv.imread(imgDownload.Filename);
match({url,
img1,
img2,
detector: new cv.ORBDetector(),
matchFunc: cv.matchBruteForceHamming,
});
})
})(i);
}
In the above code, imgDownload
is an async function which will download image, match
will match features of downloaded image with another image. Need to execute a function after this for-loop
.
How to restructure this code, using Promise
or async-await
, so that each call to asynchronous function is waited for and only after completing the for-loop
, flow continues?
Edited the code with async-await
:
FolderPath.forEach(FolderPath => {
(async function () {
var filename = await imgDownload(FolderPath.FolderPath);
var url = FolderPath.FolderPath;
const img2 = cv.imread(filename);
imgs = await match({url,
img1,
img2,
detector: new cv.ORBDetector(),
matchFunc: cv.matchBruteForceHamming,
});
})();
});
function someFunction(){
console.log("After forEach");
}
How to execute someFunction
only after forEach
?