How can I limit Q promise concurrency?

前端 未结 4 1298
时光取名叫无心
时光取名叫无心 2020-12-09 19:31

How do I write a method that limits Q promise concurrency?

For instance, I have a method spawnProcess. It returns a Q promise.
I want no more than 5

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-09 20:11

    I have a library that does this for you https://github.com/ForbesLindesay/throat

    You can use it via browserify or download the standalone build from brcdn (https://www.brcdn.org/?module=throat&version=latest) and add it as a script tag.

    Then (assuming the Promise constructor is polyfilled or implemented in your environment) you can do:

    //remove this line if using standalone build
    var throat = require('throat');
    
    function limitConcurrency(promiseFactory, limit) {
      var fn = throat(promiseFactory, limit);
      return function () {
        return Q(fn.apply(this, arguments));
      }
    }
    

    You could just call throat(promiseFactory, limit) directly but that would return a promise promise rather than a Q promise.

    I also really like using it with array.map.

    // only allow 3 parallel downloads
    var downloadedItems = Q.all(items.map(throat(download, 3)));
    

提交回复
热议问题