0

I get and issue using parallel() function in Async module...

The array images is empty...

var asyncTasks = [];

switch(type){
  case 'instafilm':

    var newFileName;
    var images = [];

    selectedPhotos.forEach(function(id){
      newFileName = pathDir + '/' + id + '.jpg';
      asyncTasks.push(function(callback){
        self.api.media(id, function(err, media, remaining, limit) {
          images.push(media.images.thumbnail.url);
        });
      });
    });

    asyncTasks.push( function(callback){
      console.log(images); // empty
    });


    break;
}

console.log(images); // empty

EDIT #1 :

var asyncTasks = [];

var newFileName;
var images = [];

selectedPhotos.forEach(function(id){
  newFileName = pathDir + '/' + id + '.jpg';
  asyncTasks.push(function(callback){
    self.api.media(id, function(err, media, remaining, limit) {
      images.push(media.images.thumbnail.url);
      callback(null);
    });
  });
});

asyncTasks.push( function(callback){
  console.log(images); // before creating .zip I need to write files using images[]
  gm()
  .in('-page', '+0+0')
  .in('./public/images/instafilm.jpg')
  .in('-page', '+10+10')
  .in(images[0])
  .in('-page', '+10+30')
  .in(images[1])
  .in('-page', '+10+60')
  .in(images[2])
  .in('-page', '+10+90')
  .in(images[3])
  .mosaic()
  .minify()
  .write(newFileName, function (err) {
    if (!err) console.log('done');
    if (err) console.log(err);
    callback();
  });
});



async.parallel(asyncTasks, function(){
    // here I write a .zip file
    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

1

If I understand your snippet right, your callback function is never called in :

self.api.media(id, function(err, media, remaining, limit) {
  images.push(media.images.thumbnail.url);
});

which is a problem.

Another issue I can think of is that you call console.log(images) just after executing this snippet (so async code have not been executed yet) and as a part of your parallel call. But parallel as it's name implies (and it's doc confirm) run your tasks "parallel" (as much as node allows it).

So, especially since console.log() is synchronous, your media([...]) call wouldn't have time to finish before the console call.

If I guess right, you then need to add a callback like this :

function(callback){
  self.api.media(id, function(err, media, remaining, limit) {
    images.push(media.images.thumbnail.url);
    callback(null);
  });
}

And to check images content after your async code finish like :

parallel(asyncTasks,function(){
  console.log(images)
})

Edit : According to new code snippet :

first part of the code look OK, until gm call wich should be done as this :

async.parallel(asyncTasks, function(){
  gm()
  .in('-page', '+0+0')
  .in('./public/images/instafilm.jpg')
  .in('-page', '+10+10')
  .in(images[0])
  .in('-page', '+10+30')
  .in(images[1])
  .in('-page', '+10+60')
  .in(images[2])
  .in('-page', '+10+90')
  .in(images[3])
  .mosaic()
  .minify()
  .write(newFileName, function (err) {
    if (!err) console.log('done');
    if (err) console.log(err);
    // here I write a .zip file
    var admZip = new AdmZip();
    var pathDir = './public/uploads/'+reference;
    admZip.addLocalFolder(pathDir);
    var willSendthis = admZip.toBuffer();
    admZip.writeZip('./public/uploads/'+reference+'.zip');   
  });
});
Sign up to request clarification or add additional context in comments.

4 Comments

But your callback function (where you make a zip) is called? Also, what is your gm() function doing? it looks like it should be called after your asyncTasks finished, just before you make your zip. Here, it's called during your Images array construction.
Yes the .zip construction is called because I get an empty .zip file... My gm() function is writing a file in a specific folder...
Try calling gm in your final callback (where you call admZip). This way your images array will have been filled when you call gm. Then do your admZip call in gm's write callback. Answer edited to explain this
Doesn't work... My .zip file is empty when I put the GM() function just before the .zip construction...

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.