Skip to content

Commit 68b6ba6

Browse files
requested changes
1 parent 62d1acc commit 68b6ba6

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

src/cursor/abstract_cursor.ts

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,8 @@ export abstract class AbstractCursor<
281281
}
282282

283283
/** Returns current buffered documents */
284-
readBufferedDocuments(number?: number): TSchema[] {
285-
const bufferedDocs: TSchema[] = [];
284+
readBufferedDocuments(number?: number): NonNullable<TSchema>[] {
285+
const bufferedDocs: NonNullable<TSchema>[] = [];
286286
const documentsToRead = Math.min(
287287
number ?? this.documents?.length ?? 0,
288288
this.documents?.length ?? 0
@@ -459,27 +459,21 @@ export abstract class AbstractCursor<
459459
*/
460460
async toArray(): Promise<TSchema[]> {
461461
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
464464
for await (const document of this) {
465465
array.push(document);
466-
let docs = this.readBufferedDocuments();
466+
const docs = this.readBufferedDocuments();
467467
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);
477473
}
478-
array.push(...docs);
479474
}
480475
return array;
481476
}
482-
483477
/**
484478
* Add a cursor flag to the cursor
485479
*
@@ -820,7 +814,7 @@ export abstract class AbstractCursor<
820814
}
821815

822816
/** @internal */
823-
private async transformDocument(document: NonNullable<TSchema>): Promise<TSchema> {
817+
private async transformDocument(document: NonNullable<TSchema>): Promise<NonNullable<TSchema>> {
824818
if (this.transform == null) return document;
825819

826820
try {

0 commit comments

Comments
 (0)