Uploaded image for project: 'Node.js Driver'
  1. Node.js Driver
  2. NODE-268

Add Cursor#forEach method with standard `callback` argument

    • Type: Icon: New Feature New Feature
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • 2.0
    • Affects Version/s: 2.0
    • Component/s: None
    • 5

      The Cursor#each method only accepts a single argument, a function which might be called multiple times, and thus lacks a Node.js-style callback argument that is called exactly once.

      An Array#forEach style method with an additional `callback` argument would give asynchronous iteration a proper Node.js callback by getting called exactly once:

      cursor.forEach(iterator, callback);
      function iterator(document){
          // Called zero or more times, once per document in result set
      }
      function callback(err){
          // Called exactly once, after all documents have been iterated over, or when there is an error
      }
      

      Using this function would increase readability and save a couple lines of code.

      The functionality would probably be equivalent to:

      Cursor.prototype.forEach = function forEach(iterator, callback){
          this.each(function(err, doc){
              if(err) callback(err);
              else if(doc) iterator(doc);
              else callback(null);
          });
      }
      

            Assignee:
            christkv Christian Amor Kvalheim
            Reporter:
            awwright Austin Wright
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: