-
Type: New Feature
-
Resolution: Done
-
Priority: Minor - P4
-
Affects Version/s: 2.0
-
Component/s: None
-
5
-
Empty show more show less
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); }); }