Created
November 15, 2011 17:44
-
-
Save aheckmann/1367755 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// today | |
Thing.find({ name: {$in: someNames }}).each(function (err, doc, next) { | |
if (err) return done(err); | |
if (!doc) return done(); | |
doSomethingAsync(doc, function (err) { | |
if (err) return done(err); | |
// we are handling iteration manually by using `next` | |
next(); | |
}); | |
}); | |
// tomorrow | |
var cursor = Thing.find({ name: {$in: someNames }}).cursor(); | |
cursor.on('end', done); // the cursor is now closed - no memory leaks | |
cursor.on('error', done); // the cursor is now closed - no memory leaks | |
cursor.on('doc', function (doc, next) { | |
var self = this; | |
doSomethingAsync(doc, function (err) { | |
if (err) return self.emit('error', err); | |
// `next` is still optional, if not passed then auto iteration | |
// would take place the same as today. | |
next(); | |
}); | |
}); | |
// or | |
cursor.on('doc', function (doc) { | |
// i decide that i'm finished with this cursor. | |
this.close(); | |
// no more doc events fire. | |
// underlying cursor is closes - memory cleaned up | |
// this fires `end` event | |
// TODO do we need to fire `close`? | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment