I need to resize some images. Problem is the library I'm using do one resize per callback.
I want to resize several images so I put it in a loop:
exports.resizeImages = function (req, res) {
var images = fs.readdirSync('uploads/');
for (var n = 0; n < files.length; n++) {
var tgt = 'uploads/resized/' + images[n];
gm(tgt).resize(150).write(tgt, function (err) {
if (err) {
console.log('resizing failed');
res.status(400).send('failed to resize');
return;
}
if (n == images.length) {
res.status(200).send();
}
});
}
}
I'm aware I can't do it like this. I need make the loop wait until the callback responds somehow. I've seen some examples but I can't get it to work.
Any ideas?
You could also use the node async module
Something like this: