@@ -281,8 +281,8 @@ export abstract class AbstractCursor<
281
281
}
282
282
283
283
/** Returns current buffered documents */
284
- readBufferedDocuments ( number ?: number ) : TSchema [ ] {
285
- const bufferedDocs : TSchema [ ] = [ ] ;
284
+ readBufferedDocuments ( number ?: number ) : NonNullable < TSchema > [ ] {
285
+ const bufferedDocs : NonNullable < TSchema > [ ] = [ ] ;
286
286
const documentsToRead = Math . min (
287
287
number ?? this . documents ?. length ?? 0 ,
288
288
this . documents ?. length ?? 0
@@ -459,27 +459,21 @@ export abstract class AbstractCursor<
459
459
*/
460
460
async toArray ( ) : Promise < TSchema [ ] > {
461
461
const array : TSchema [ ] = [ ] ;
462
-
463
- // when each loop iteration ends,documents will be empty and a 'await (const document of this)' will run a getMore operation
462
+ // at the end of the loop (since readBufferedDocuments is called) the buffer will be empty
463
+ // then, the 'await of' syntax will run a getMore call
464
464
for await ( const document of this ) {
465
465
array . push ( document ) ;
466
- let docs = this . readBufferedDocuments ( ) ;
466
+ const docs = this . readBufferedDocuments ( ) ;
467
467
if ( this . transform != null ) {
468
- docs = await Promise . all (
469
- docs . map ( async doc => {
470
- if ( doc != null ) {
471
- return await this . transformDocument ( doc ) ;
472
- } else {
473
- throw Error ;
474
- }
475
- } )
476
- ) ;
468
+ for ( const doc of docs ) {
469
+ array . push ( await this . transformDocument ( doc ) ) ;
470
+ }
471
+ } else {
472
+ array . push ( ...docs ) ;
477
473
}
478
- array . push ( ...docs ) ;
479
474
}
480
475
return array ;
481
476
}
482
-
483
477
/**
484
478
* Add a cursor flag to the cursor
485
479
*
@@ -820,7 +814,7 @@ export abstract class AbstractCursor<
820
814
}
821
815
822
816
/** @internal */
823
- private async transformDocument ( document : NonNullable < TSchema > ) : Promise < TSchema > {
817
+ private async transformDocument ( document : NonNullable < TSchema > ) : Promise < NonNullable < TSchema > > {
824
818
if ( this . transform == null ) return document ;
825
819
826
820
try {
0 commit comments