Skip to content

Instantly share code, notes, and snippets.

@cadorn
Created February 2, 2012 19:39
Show Gist options
  • Save cadorn/1725325 to your computer and use it in GitHub Desktop.
Save cadorn/1725325 to your computer and use it in GitHub Desktop.

Revisions

  1. cadorn revised this gist Feb 2, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions q-throttle.js
    Original file line number Diff line number Diff line change
    @@ -17,6 +17,7 @@
    *
    * for (var i=0 ; i < 10 ; i++) {
    * throttle.when([i], function(i) {
    * // Never more than 3 unresolved doDeferredWork() promises
    * return doDeferredWork(i).then(function() {
    * });
    * });
  2. cadorn revised this gist Feb 2, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions q-throttle.js
    Original file line number Diff line number Diff line change
    @@ -9,6 +9,7 @@
    *
    * var Q = require("q");
    *
    * // Require this module (to add `Q.Throttle` to the `Q` API)
    * Q.Throttle = require("q-throttle").Throttle;
    *
    * // Maximum of 3 unresolved promises at a time
  3. cadorn renamed this gist Feb 2, 2012. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. cadorn revised this gist Feb 2, 2012. 1 changed file with 134 additions and 1 deletion.
    135 changes: 134 additions & 1 deletion gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1 +1,134 @@
    .
    /**
    * Copyright: 2012 Christoph Dorn <[email protected]>
    * License: MIT
    * Source: https://gist.github.com/1725325
    *
    * NodeJS Example:
    *
    * // Requirements: `npm install q`
    *
    * var Q = require("q");
    *
    * Q.Throttle = require("q-throttle").Throttle;
    *
    * // Maximum of 3 unresolved promises at a time
    * var throttle = Q.Throttle(3);
    *
    * for (var i=0 ; i < 10 ; i++) {
    * throttle.when([i], function(i) {
    * return doDeferredWork(i).then(function() {
    * });
    * });
    * }
    *
    * throttle.on("done", function()
    * {
    * });
    *
    */


    var Q = require("q"),
    EVENTS = require("events");


    var Throttle = exports.Throttle = function(max)
    {
    if (!(this instanceof Throttle))
    return new Throttle(max);

    this.count = 0;
    this.buffer = [];
    this.max = max;
    };
    Throttle.prototype = new EVENTS.EventEmitter();
    Throttle.prototype.when = function(args, func)
    {
    var self = this,
    result;

    if (self.count >= self.max)
    {
    self.buffer.push([args, func]);
    return;
    }
    self.count += 1;

    result = func.apply(null, args);

    if (!Q.isPromise(result))
    {
    throw new Error("Throttled function call did not return a promise!");
    }

    Q.when(result, function()
    {
    self.count -= 1;
    if (self.buffer.length > 0)
    {
    var info = self.buffer.shift();
    self.when(info[0], info[1]);
    }
    else
    if (self.count === 0)
    {
    self.emit("done");
    }
    });
    }


    exports.Throttle_Test = function()
    {
    var deferred = Q.defer();

    try
    {
    // Maximum of 3 unresolved promises at a time
    var throttle = new Throttle(3),
    list = [],
    i,
    count = 0;

    console.log("Running Throttle_Test:");

    for (i=0 ; i < 10 ; i++)
    {
    list.push(function()
    {
    return Q.delay(200 + (100 * i%3/10));
    });
    }

    for (i=0 ; i < list.length ; i++)
    {
    console.log(" triggering " + i);
    throttle.when([i], function(i)
    {
    // Only 3 unresolved promises will run at a time
    console.log(" starting " + i);
    count += 1;
    if (count > 3) throw new Error("More than 3 unresolved promises at the same time!");
    return list[i]().then(function()
    {
    console.log(" " + i + " done");
    count -= 1;
    });
    });
    }

    console.log(" triggering done");

    throttle.on("done", function()
    {
    console.log("Throttle_Test OK!");
    deferred.resolve();
    });
    }
    catch(e)
    {
    deferred.reject(e);
    }

    return deferred.promise;
    }
  5. cadorn created this gist Feb 2, 2012.
    1 change: 1 addition & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    .