我在 Async 模块中使用 parallel() 函数获取并发出问题...
数组images
是空的...
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
编辑#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');
});