File tree Expand file tree Collapse file tree 3 files changed +44
-0
lines changed Expand file tree Collapse file tree 3 files changed +44
-0
lines changed Original file line number Diff line number Diff line change @@ -327,6 +327,11 @@ This is a major release and breaks backwards compatibility.
327327 The method takes a document or a document key and returns a fully qualified
328328 document ID string for the document in the current collection.
329329
330+ - Added support for ` for await ` in ` ArrayCursor `
331+
332+ It is now possible to use ` for await ` to iterate over each item in a cursor
333+ asynchronously.
334+
330335- Improved type signatures for TypeScript
331336
332337 Most methods should now provide full type signatures for options and response
Original file line number Diff line number Diff line change @@ -100,6 +100,34 @@ export class ArrayCursor<T = any> {
100100 return this . hasMore || Boolean ( this . _result . length ) ;
101101 }
102102
103+ /**
104+ * Enables use with `for await` to exhaust the cursor by asynchronously
105+ * yielding every value in the cursor's remaining result set.
106+ *
107+ * **Note**: If the result set spans multiple batches, any unfetched batches
108+ * will only be fetched on demand. Depending on the cursor's TTL and the
109+ * processing speed, this may result in the server discarding the cursor
110+ * before it is fully depleted.
111+ *
112+ * @example
113+ * ```js
114+ * const cursor = await db.query(aql`
115+ * FOR user IN users
116+ * FILTER user.isActive
117+ * RETURN user
118+ * `);
119+ * for await (const user of cursor) {
120+ * console.log(user.email, user.isAdmin);
121+ * }
122+ * ```
123+ */
124+ async * [ Symbol . asyncIterator ] ( ) : AsyncGenerator < T , undefined , undefined > {
125+ while ( this . hasNext ) {
126+ yield this . next ( ) as Promise < T > ;
127+ }
128+ return undefined ;
129+ }
130+
103131 /**
104132 * TODO
105133 */
Original file line number Diff line number Diff line change @@ -30,6 +30,17 @@ describe("Cursor API", () => {
3030 beforeEach ( async ( ) => {
3131 cursor = await db . query ( aqlQuery ) ;
3232 } ) ;
33+ describe ( "for await of cursor" , ( ) => {
34+ it ( "returns each next result of the Cursor" , async ( ) => {
35+ let i = 0 ;
36+ for await ( const value of cursor ) {
37+ expect ( value ) . to . equal ( aqlResult [ i ] ) ;
38+ i += 1 ;
39+ }
40+ expect ( i ) . to . equal ( aqlResult . length ) ;
41+ expect ( cursor . hasNext ) . to . equal ( false ) ;
42+ } ) ;
43+ } ) ;
3344 describe ( "cursor.all" , ( ) => {
3445 it ( "returns an Array of all results" , async ( ) => {
3546 const values = await cursor . all ( ) ;
You can’t perform that action at this time.
0 commit comments