0

I'd like to know how to execute async functions, my goal is to uploads files and compress them... But it doesn't work because my files are not yet uploading when I write the .zip...

SO I get an empty .zip file...

var asyncTasks = [];

selectedPhotos.forEach(function(id){
  asyncTasks.push(function(callback){
    var newFileName = pathDir + '/' + id + '.jpg';
    api.media(id, function(err, media, remaining, limit) {
      gm()
      .in('-page', '+0+0')
      .in('./public/images/instabox.jpg')
      .in('-page', '+10+10')
      .in(media.images.thumbnail.url)
      .mosaic()
      .minify()
      .write(newFileName, function (err) {
        if (!err) console.log('done');
        if (err) console.log(err);
      });
    });
    callback();
  });
});

async.parallel(asyncTasks, function(){

  var admZip = new AdmZip();
  var pathDir = './public/uploads/'+reference;
  admZip.addLocalFolder(pathDir);
  var willSendthis = admZip.toBuffer();
  admZip.writeZip('./public/uploads/'+reference+'.zip');

});

1 Answer 1

2

You're calling the callback() too early. Move callback(); inside of your .write() callback like so:

.write(newFileName, function (err) {
  if (!err) console.log('done');
  if (err) console.log(err);
  callback(err);
});

When you execute the callback, that signifies that the task is finished. So without the above change, you're basically telling async that you're done immediately.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.