I'm getting stuck in problem with asynchronous of nodejs. I want to resize on images in folder, resize is an executable binary file. The problem is my resize cannot be executed multiple times at same time. so that I use Array.prototype.forEach instead of async.forEach to expect that each file will be processed one by one.
var exec = require('child_process').exec;
exec('ls ' + IMAGE_FOLDER, function (error, stdout, stderr) {
if (error) {throw error;}
var fileList = stdout.split("\n");
fileList.pop(); //Remove the last element that null
fileList.forEach(function(imageFile, index, array) {
var inFile = IMAGE_FOLDER + imageFile;
console.log(inFile);
exec('resize ' + inFile, function(err, stdout, stderr){
if (err) {
console.log(stderr);
throw err;
}
console.log('resized ' + imageFile );
})
});
});
But the result I got is the behavior of my code is none-block, it prints out:
image1
image2
...
resized image1
resized image2
...
I expected the behavior of printout should be:
image1
resize image1
image2
resize image2
...
Please show me where I was wrong. Any help is vary appreciate.
via Ngoan Tran
No comments:
Post a Comment