Skip to content

Commit

Permalink
Implement Symbol.asyncIterator
Browse files Browse the repository at this point in the history
  • Loading branch information
pluma committed Aug 6, 2019
1 parent e90e13a commit 317a86f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ export class ArrayCursor {
}
}

[Symbol.asyncIterator]() {
return {
next: async () => {
if (!this._result.length && !this._hasMore) {
return { done: true, value: undefined };
}
const value = await this.next();
return { done: false, value };
}
};
}

async all() {
await this._drain();
let result = this._result;
Expand Down
11 changes: 11 additions & 0 deletions src/test/08-cursors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ describe("Cursor API", () => {
expect(val2).to.equal(1);
});
});
describe("for await of cursor", () => {
it("returns each next result of the Cursor", async () => {
let i = 0;
for await (const value of cursor) {
expect(value).to.equal(aqlResult[i]);
i += 1;
}
expect(i).to.equal(aqlResult.length);
expect(cursor.hasNext()).to.equal(false);
});
});
describe("cursor.hasNext", () => {
it("returns true if the Cursor has more results", async () => {
expect(cursor.hasNext()).to.equal(true);
Expand Down

0 comments on commit 317a86f

Please sign in to comment.