javascript - Using async package inside blue bird promises -


i know if i'm using async inside promise, this:

new promise(function(resolve, reject) {  async.maplimit(files, 3000, function(file, callback) {    //    //  }, function(result) {    //  }); }); 

i'm doing because need process maximum of 3000 files, if pass 9000, need process before call other function in stack.

for example i'm using new promise, i'm using promisifyall in real code. "same"


i have mainobject:

var myobject = {} 

inside with, have 4 steps, , i'm using promisifyall because need wait 1 step finish before call other, this:

var myobject = {   stepone: function(files, callback) {   },    steptwo: function(files, callback) {   },    stepthree: function(files, callback) {   },    stepfour: function(files, callback) {   } };  promise.promisifyall(myobject); 

the problem is, in step (i'm dealing files here) can allow program run 3.000 asynchronous, achieve this, i'm using following:

async.maplimit(files, 3000, function() {  }, function(result) { }); 

so, final code be:

var myobject = {   stepone: function(files, callback) {     async.maplimit(files, 3000, function() {       }, function(result) {     });   },    steptwo: function(files, callback) {     async.maplimit(files, 3000, function() {       }, function(result) {     });   },    stepthree: function(files, callback) {     async.maplimit(files, 3000, function() {       }, function(result) {     });   },    stepfour: function(files, callback) {     async.maplimit(files, 3000, function() {       }, function(result) {     });   } }; 

just use bluebird's promise.map() , pass concurrency option:

promise.map(files, function(file) {     // process file here, return result or promise of result }, {concurrency: 3000}).then(...) 

documentation , code example here.


you could, of course use async inside promise, though not recommend it:

var p = new promise(function(resolve, reject) {  async.maplimit(files, 3000, function(file, callback) {    //    //    callback(null, result);  }, function(err, results) {      if (err) {          reject(err);      } else {          resolve(results);      }  }); }); 

again, recommend using promise.map():

var myobject = {   stepone: function(files) {     return promise.map(files, function(file) {         // process file         // return result or promise of result     }, {concurrency: 3000});   },    steptwo: function(files) {     return promise.map(files, function(file) {         // process file         // return result or promise of result     }, {concurrency: 3000});   },    stepthree: function(files) {     return promise.map(files, function(file) {         // process file         // return result or promise of result     }, {concurrency: 3000});   },    stepfour: function(files) {     return promise.map(files, function(file) {         // process file         // return result or promise of result     }, {concurrency: 3000}); }; 

then, there no need promisifyall() methods use promises.

myobject.stepone(files).then(myobject.steptwo).then(myobject.stepthree).then(myobject.stepfour); 

Comments

Popular posts from this blog

matlab - error with cyclic autocorrelation function -

django - (fields.E300) Field defines a relation with model 'AbstractEmailUser' which is either not installed, or is abstract -

c# - What is a good .Net RefEdit control to use with ExcelDna? -