创建具有蓝鸟承诺的节流功能

Creating throttling function with Bluebird promises

本文关键字:功能 承诺 蓝鸟 创建      更新时间:2023-09-26

我正在尝试创建一个限制函数。我查看了一些SO帖子并复制了一些代码,但我无法延迟它。

基本上,我在一个类中有许多方法需要调用Amazon API。它们都使用了一个通用函数 - doCall我实现如下:

Amazon.prototype.doCall = function(func) {
  var self = this;
  var queue = P.resolve();
  function throttle(fn) {
    var res = queue.then(function() { // wait for queue
      return fn(); // call the function
    });
    queue = P.delay(61000).return(queue); // make the queue wait for 61 seconds
    return res; // return the result
  }

  // Create instance of MWS client
  if (!this.client) {
    this.client = new mws.Client(key, secret, merchant, {});
  }
  var call = function() {
    // The library uses a weird signature so I am wrapping it thus
    return new P(function(resolve, reject) {
      // The original MWS library call
      self.client.invoke(func, function(r, e) {
        // ... stuff
        resolve(r);
      });
    });
  };
  return throttle(call);
};

基本上我获取订单列表和订单,需要延迟每次调用 60+ 秒。现在,这一切都毫不拖延地发生。建议?

我基本上是这样使用它的(人为的,但应该给出这个想法)

self.doCall(ListOrders).then(function(res) { 
  // parse results
  self.doCall(ListMoreOrdersByPage).then(function(res) {
     // Now I might go through each and fetch details
     var ids = [...] // Parse result for ids
     return P.map(ids, function(id) {
       return doCall(GetOrderById);
     });  
     ....

你的问题是

Amazon.prototype.doCall = function(func) {
    var queue = P.resolve();
    …

意味着您在该方法的每次调用上重新创建一个新queue。不是很有帮助。相反,您可能希望每个Amazon一个队列,因此将初始化放在构造函数中。

我还简化了您的代码:

function Amazon(…) {
  …
  this.queue = P.resolve();
}
Amazon.prototype.doCall = function(func) {
  if (!this.client) {
    // Create instance of MWS client
    this.client = new mws.Client(key, secret, merchant, {});
  }
  var self = this;
  var res = this.queue.then(function() {
    // The library uses a weird signature so I am wrapping it thus
    return new P(function(resolve, reject) {
      self.client.invoke(func, function(r, e) {
        // ... stuff
        resolve(r);
      });
    });
  });
  this.queue = this.queue.delay(610000); // make the queue wait for 61s
  // if you want to make it wait *between* calls, use
  // this.queue = res.catch(function(){}).delay(610000);
  return res;
};
我想

在创建一个实际的工作解决方案后再回来。让我搞砸的技巧是你需要将未调用的函数传递到队列器中。我知道这不是 OP 所要求的,但我希望这可以帮助其他寻找可扩展解决方案的人。

你可以在这里看到它的实际效果:http://jsbin.com/seqeqecate/5/

function asyncFunction(){
  var deferred = Promise.defer();
  setTimeout(function(){
    console.log('ping');
    deferred.resolve();  
  },3000);
  return deferred.promise;
}
function AsyncQueuer(){
  var queue = [],
      running = false;
  function runQueue(){
    var first = queue.shift();
    running = true;
    if (first) {
      first.promisable.apply(first.context).then(function() {
        first.deferral.resolve();
        runQueue();
      }, 
      function() {
        first.deferral.reject();
        runQueue();
      });
    } else {
      running = false;
    }
  }
  return {
    add: function(promisable, context) {
      var deferred = Promise.defer();
      queue.push({
        promisable: promisable,
        deferral: deferred,
        context: context || window
      });
      if (!running) {
        runQueue();
      }
      return deferred;
    }
  };
}
var asyncQueuer = new AsyncQueuer();
asyncQueuer.add(asyncFunction);
asyncQueuer.add(asyncFunction);
asyncQueuer.add(asyncFunction).then(function(){}).fail(function(){});