/*
 * Author: CM
 */

(function(global) {

  /**
   * @callback PromiseThrottled
   * @param {*} any number of optional params
   * @return {Promise}
   */

  /**
   * @param {PromiseThrottled} fn
   * @param {Object|null} [options]
   * @param {Boolean} options.cancelLeading Whether to cancel the previous promise if it is still running.
   * @param {Boolean} options.queue Whether to queue the next promises.
   * @param {String} options.key A custom key to store the resulted PromiseThrottled.
   * @returns {PromiseThrottled}
   */
  function promiseThrottler(fn, options) {
    options = _.defaults(
      options || {}, {
        cancelLeading: false,
        queue: false
      }
    );
    if ((+options.cancelLeading) + (+options.queue) > 1) {
      throw new Error('PromiseThrottler options "cancelLeading", "queue" exclude each other.');
    }
    if (options.key) {
      return namespaceThrottler(options.key, fn, options);
    } else {
      return nonameThrottler(fn, options);
    }
  }

  /**
   * @see promiseThrottler
   */
  function nonameThrottler(fn, options) {
    var promise;

    return function() {
      var cancelLeading = options.cancelLeading && promise && promise.isPending() && promise.isCancellable();
      if (cancelLeading) {
        promise.cancel();
      }
      var addToQueue = options.queue && promise && promise.isPending();
      if (cancelLeading || addToQueue) {
        var args = arguments;
        var self = this;
        var leadingPromise = promise;
        promise = new Promise(function(resolve) {
          leadingPromise.finally(function() {
            resolve(fn.apply(self, args));
          });
        });
        return promise;
      }
      if (!promise || !promise.isPending()) {
        promise = fn.apply(this, arguments);
      }
      return promise;
    };
  }

  var storage = {};

  /**
   * @see promiseThrottler
   */
  function namespaceThrottler(namespace, fn, options) {
    if (!storage[namespace]) {
      storage[namespace] = nonameThrottler(fn, options);
    }
    return storage[namespace];
  }

  /**
   * @param {String} namespace
   */
  function removeThrottler(namespace) {
    if (storage[namespace]) {
      delete storage[namespace];
    }
  }

  global.promiseThrottler = promiseThrottler;
  global.removePromiseThrottler = removeThrottler;

})(window);

@@ -1131,61 +1131,21 @@
     
-var cancelLeading = options.cancelLeading && 
+if (!
 promise 
 && p
@@ -1132,35 +1132,36 @@
    if (!promise 
-&& 
+%7C%7C !
 promise.isPendin
@@ -1167,35 +1167,136 @@
 ng()
- && promise.isCancellable()
+) %7B%0A        promise = fn.apply(this, arguments);%0A        return promise;%0A      %7D%0A      var cancelLeading = options.cancelLeading
 ;%0A  
@@ -1394,42 +1394,8 @@
 ueue
- && promise && promise.isPending()
 ;%0A  
@@ -1727,107 +1727,8 @@
   %7D%0A
-      if (!promise %7C%7C !promise.isPending()) %7B%0A        promise = fn.apply(this, arguments);%0A      %7D%0A
     

Streamline PromiseThrottler