Using async package inside blue bird promises

时间:2015-10-30 21:28:29

标签: javascript asynchronous bluebird

I would like to know if I'm using a async inside a promise, like this:

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

I'm doing this because I need process an maximum of 3000 files, but if I pass 9000, I need process everything before call the other function in my stack.

For the example I'm using the new Promise, but I'm using promisifyAll in my real code. Which is the "same"


I have this MainObject:

var MyObject = {}

Inside with, I have 4 steps, and I'm using promisifyAll because I need wait one step finish before call the other, like 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 which step (I'm dealing with files here) I just can allow the program run 3.000 Asynchronous, so to achieve this, I'm using the following:

async.mapLimit(files, 3000, function() {

}, function(result) {
});

So, the final code would 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) {
    });
  }
};

1 个答案:

答案 0 :(得分:0)

Just use Bluebird's Promise.map() and pass the concurrency option:

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

Documentation and code example here.


You could, of course use async inside a promise, though I would 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, I recommend using Promise.map():

var MyObject = {
  stepOne: function(files) {
    return Promise.map(files, function(file) {
        // process a file
        // return result or promise of result
    }, {concurrency: 3000});
  },

  stepTwo: function(files) {
    return Promise.map(files, function(file) {
        // process a file
        // return result or promise of result
    }, {concurrency: 3000});
  },

  stepThree: function(files) {
    return Promise.map(files, function(file) {
        // process a file
        // return result or promise of result
    }, {concurrency: 3000});
  },

  stepFour: function(files) {
    return Promise.map(files, function(file) {
        // process a file
        // return result or promise of result
    }, {concurrency: 3000});
};

Then, there is no need for promisifyAll() as the methods already use promises.

MyObject.stepOne(files).then(MyObject.stepTwo).then(MyObject.stepThree).then(MyObject.stepFour);