title: Creating throttling function with Bluebird promises
body: <p>I am attempting to created a throttling function. I have looked at a few SO posts and copied some code but I am unable to get it to delay.</p>
  
  <p>Basically I have a number of methods in a class that need to call the Amazon API. They all make use of a common function - <code>doCall</code> which I implemented as follows:</p>
  
  <pre><code>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);
  };
  </code></pre>
  
  <p>Basically I fetch order lists and orders and need to delay each call 60+ seconds. Right now it all happens without a delay whatsoever. Suggestions?</p>
  
  <p>I am basically using it like this (contrived but should give the idea)</p>
  
  <pre><code>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);
       });  
       ....
  </code></pre>