@@ -120,6 +120,8 @@ var Cursor = function(bson, ns, cmd, options, topology, topologyOptions) {
120
120
, topologyOptions : topologyOptions
121
121
// Promise library
122
122
, promiseLibrary : promiseLibrary
123
+ // Current doc
124
+ , currentDoc : null
123
125
}
124
126
125
127
// Legacy fields
@@ -166,12 +168,71 @@ for(var name in CoreCursor.prototype) {
166
168
Cursor . prototype [ name ] = CoreCursor . prototype [ name ] ;
167
169
}
168
170
171
+ /**
172
+ * Check if there is any document still available in the cursor
173
+ * @method
174
+ * @param {Cursor~resultCallback } callback The result callback.
175
+ * @throws {MongoError }
176
+ * @deprecated
177
+ * @return {Promise } returns Promise if no callback passed
178
+ */
179
+ Cursor . prototype . hasNext = function ( callback ) {
180
+ var self = this ;
181
+
182
+ // Execute using callback
183
+ if ( typeof callback == 'function' ) return nextObject ( self , function ( err , doc ) {
184
+ if ( ! doc ) return callback ( null , false ) ;
185
+ self . s . currentDoc = doc ;
186
+ callback ( null , true ) ;
187
+ } ) ;
188
+
189
+ // Return a Promise
190
+ return new this . s . promiseLibrary ( function ( resolve , reject ) {
191
+ nextObject ( self , function ( err , doc ) {
192
+ if ( self . s . state == Cursor . CLOSED || self . isDead ( ) ) return resolve ( false ) ;
193
+ if ( err ) return reject ( err ) ;
194
+ if ( ! doc ) return resolve ( false ) ;
195
+ self . s . currentDoc = doc ;
196
+ resolve ( true ) ;
197
+ } ) ;
198
+ } ) ;
199
+ }
200
+
201
+
202
+ /**
203
+ * Get the next available document from the cursor, returns null if no more documents are available.
204
+ * @method
205
+ * @param {Cursor~resultCallback } callback The result callback.
206
+ * @throws {MongoError }
207
+ * @deprecated
208
+ * @return {Promise } returns Promise if no callback passed
209
+ */
169
210
Cursor . prototype . next = function ( callback ) {
170
211
var self = this ;
171
- if ( typeof callback == 'function' ) return this . _next ( callback ) ;
212
+
213
+ // Execute using callback
214
+ if ( typeof callback == 'function' ) {
215
+ // Return the currentDoc if someone called hasNext first
216
+ if ( self . s . currentDoc ) {
217
+ var doc = self . s . currentDoc ;
218
+ self . s . currentDoc = null ;
219
+ return callback ( null , doc ) ;
220
+ }
221
+
222
+ // Return the next object
223
+ return nextObject ( self , callback )
224
+ } ;
225
+
172
226
// Return a Promise
173
227
return new this . s . promiseLibrary ( function ( resolve , reject ) {
174
- self . _next ( function ( err , r ) {
228
+ // Return the currentDoc if someone called hasNext first
229
+ if ( self . s . currentDoc ) {
230
+ var doc = self . s . currentDoc ;
231
+ self . s . currentDoc = null ;
232
+ return resolve ( doc ) ;
233
+ }
234
+
235
+ nextObject ( self , function ( err , r ) {
175
236
if ( err ) return reject ( err ) ;
176
237
resolve ( r ) ;
177
238
} ) ;
@@ -360,15 +421,7 @@ Cursor.prototype.skip = function(value) {
360
421
* The callback format for results
361
422
* @callback Cursor~resultCallback
362
423
* @param {MongoError } error An error instance representing the error during the execution.
363
- * @param {(object|null) } result The result object if the command was executed successfully.
364
- */
365
-
366
- /**
367
- * Get the next available document from the cursor, returns null if no more documents are available.
368
- * @function external:CoreCursor#next
369
- * @param {Cursor~resultCallback } callback The result callback.
370
- * @throws {MongoError }
371
- * @return {Promise } returns Promise if no callback passed
424
+ * @param {(object|null|boolean) } result The result object if the command was executed successfully.
372
425
*/
373
426
374
427
/**
@@ -433,20 +486,7 @@ Cursor.prototype.skip = function(value) {
433
486
* @deprecated
434
487
* @return {Promise } returns Promise if no callback passed
435
488
*/
436
- Cursor . prototype . nextObject = function ( callback ) {
437
- var self = this ;
438
-
439
- // Execute using callback
440
- if ( typeof callback == 'function' ) return nextObject ( self , callback ) ;
441
-
442
- // Return a Promise
443
- return new this . s . promiseLibrary ( function ( resolve , reject ) {
444
- nextObject ( self , function ( err , r ) {
445
- if ( err ) return reject ( err ) ;
446
- resolve ( r ) ;
447
- } ) ;
448
- } ) ;
449
- }
489
+ Cursor . prototype . nextObject = Cursor . prototype . next ;
450
490
451
491
var nextObject = function ( self , callback ) {
452
492
if ( self . s . state == Cursor . CLOSED || self . isDead ( ) ) return handleCallback ( callback , new MongoError ( "Cursor is closed" ) ) ;
0 commit comments