Skip to content

Commit df8d9a4

Browse files
authoredNov 2, 2022
feat(NODE-4683): make ChangeStream an async iterable (#3454)
1 parent 89b27e9 commit df8d9a4

File tree

3 files changed

+393
-25
lines changed

3 files changed

+393
-25
lines changed
 

‎src/change_stream.ts

+20
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,26 @@ export class ChangeStream<
737737
}, callback);
738738
}
739739

740+
async *[Symbol.asyncIterator](): AsyncGenerator<TChange, void, void> {
741+
if (this.closed) {
742+
return;
743+
}
744+
745+
try {
746+
// Change streams run indefinitely as long as errors are resumable
747+
// So the only loop breaking condition is if `next()` throws
748+
while (true) {
749+
yield await this.next();
750+
}
751+
} finally {
752+
try {
753+
await this.close();
754+
} catch {
755+
// we're not concerned with errors from close()
756+
}
757+
}
758+
}
759+
740760
/** Is the cursor closed */
741761
get closed(): boolean {
742762
return this[kClosed] || this.cursor.closed;

0 commit comments

Comments
 (0)