Skip to content

Instantly share code, notes, and snippets.

@neilk
Last active November 25, 2021 11:06
Show Gist options
  • Save neilk/5380684 to your computer and use it in GitHub Desktop.
Save neilk/5380684 to your computer and use it in GitHub Desktop.

Revisions

  1. neilk renamed this gist Apr 14, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. neilk revised this gist Apr 14, 2013. 1 changed file with 4 additions and 5 deletions.
    9 changes: 4 additions & 5 deletions nonBlockingForEach
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,9 @@
    /**
    * Allow other processes to execute while iterating over the elements
    * of an array. Only useful if you're iterating over very large arrays,
    * or if each iteration needs to do some long-running, non-async processing.
    * Allow other processes to execute while iterating over
    * an array. Useful for large arrays, or long-running processing
    *
    * @param {Function} fn iterator that gets fed each element of the array.
    * @param {Function} next callback executed when all elements have been iterated over.
    * @param {Function} fn iterator fed each element of the array.
    * @param {Function} next executed when done
    */
    Array.prototype.nonBlockingForEach = function(fn, next) {
    var arr = this;
  3. neilk created this gist Apr 13, 2013.
    23 changes: 23 additions & 0 deletions nonBlockingForEach
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    /**
    * Allow other processes to execute while iterating over the elements
    * of an array. Only useful if you're iterating over very large arrays,
    * or if each iteration needs to do some long-running, non-async processing.
    *
    * @param {Function} fn iterator that gets fed each element of the array.
    * @param {Function} next callback executed when all elements have been iterated over.
    */
    Array.prototype.nonBlockingForEach = function(fn, next) {
    var arr = this;
    var i = 0;
    var len = arr.length;
    function iter() {
    if (i < len) {
    fn(arr[i]);
    i++;
    process.nextTick(iter);
    } else {
    next();
    }
    }
    iter();
    };