How can we use the AsyncIterator properly

I’m trying to define an AsycIterator and use it. Defining works but using it has been a problem. This is the code I have, please note that other code not related to the question has been removed

This is how I define the iterator

export interface IDriver {

  tables(): AsyncIterator<Table>
  
 // code removed
}

This is how I implement it.

export class MySql implements IDriver {
  async *tables(): AsyncIterator<Table> {
    const sql = `select table_name, table_type, table_rows 
                 from information_schema.tables 
                 where table_schema = ${this.schema} 
                 order by table_name;`

    const rows = await this.query<{
      table_name: string
      table_type: string
      table_rows: number
    }>(sql)

    for (const row of rows) {
      yield {
        name: row.table_name,
        rows: row.table_rows,
        type: row.table_type
      } as Table
    }
  }

 // ---
}

This is my package.json file

{
  "compilerOptions": {
    "target": "ESNext" ,
    "lib": [
      "ES2018.AsyncIterable",
      "DOM",
      "ESNext"
    ] ,
    "jsx": "preserve" ,
    "experimentalDecorators": true ,
    "emitDecoratorMetadata": true ,
    "useDefineForClassFields": true ,
    "moduleDetection": "auto" ,
    
    "module": "NodeNext", 
    "rootDir": "src/" ,
    "moduleResolution": "NodeNext" ,
  
    "allowUmdGlobalAccess": true ,
    "resolvePackageJsonExports": true ,
    "resolvePackageJsonImports": true ,
    "resolveJsonModule": true ,
    "checkJs": true ,
     
    "declaration": true ,
    "declarationMap": true ,
    "sourceMap": true ,
     "outDir": "./build" ,
    "removeComments": true ,
   
    "newLine": "crlf" ,
    "stripInternal": true ,
    "preserveConstEnums": true ,
      
    "allowSyntheticDefaultImports": true ,
    "esModuleInterop": true ,
    "forceConsistentCasingInFileNames": true ,

    "strict": true ,
    "noImplicitAny": true ,
    "strictNullChecks": true ,
    "strictFunctionTypes": true ,
    "strictBindCallApply": true ,
    "strictPropertyInitialization": true ,
    "noImplicitThis": true ,
    "useUnknownInCatchVariables": true ,
    "alwaysStrict": true ,
    "noUnusedLocals": true ,
    "noUnusedParameters": true ,
    "exactOptionalPropertyTypes": true ,
    "noImplicitReturns": true ,
    "noFallthroughCasesInSwitch": true ,
    "noUncheckedIndexedAccess": true ,
    "noImplicitOverride": true ,
    "noPropertyAccessFromIndexSignature": true ,
    "skipLibCheck": true ,
  },
  "include": ["./src/*"]
}

This is the usage of the above function

for await (const it of mysql.tables()) {
    print(it)
  }

which gives me a an error

src/main.ts:13:26 – error TS2504: Type ‘AsyncIterator<Table, any,
undefined>’ must have a [Symbol.asyncIterator] () method that returns
an async iterator.

If I try to explore it, this is what I find in type definitions:

interface AsyncIterable<T> {
    [Symbol.asyncIterator](): AsyncIterator<T>;
}
interface SymbolConstructor {
    /**
     * A method that returns the default async iterator for an object. Called by the semantics of
     * the for-await-of statement.
     */
    readonly asyncIterator: unique symbol;
}
declare var Symbol: SymbolConstructor;

I also tried this solution given here:

How do async iterators work? error TS2504: Type must have a ‘[Symbol.asyncIterator]()’ method that returns an async iterator

It is a 6 year old question as of now and does not work at all in my case. core-js/shim has no effect. I don’t know if the solution was only valid for a particular version of typecript. I’m using Version 5.5.4. the only way I’m able to use AsyncIterator is given below.

  const rows = await mysql.tables()
  let item = await rows.next()
  while (!item.done) {

    item = await rows.next()
  }

Which obviously is a weird way to use AsyncIterator, Really if this is the solution then perhaps not using AsyncIterator is the best option

Does anyone know what changes are required to make the code for await () work or Do i need to change my code?

Please note that I cannot return an array of my dataset because line by line processing is really needed.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await…of

The for await…of statement creates a loop iterating over async
iterable objects as well as sync iterables. This statement can only be
used in contexts where await can be used, which includes inside an
async function body and in a module.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_async_iterator_and_async_iterable_protocols

In order to be iterable, an object must implement the
Symbol.iterator method, meaning that the object (or one of the
objects up its prototype chain) must have a property with a
[Symbol.iterator] key which is available via constant Symbol.iterator:

Symbol.iterator A zero-argument function that returns an object,
conforming to the iterator protocol.

Whenever an object needs to be iterated (such as at the beginning of a
for…of loop), its Symbol.iterator method is called with no
arguments, and the returned iterator is used to obtain the values to
be iterated.

https://tc39.es/ecma262/#sec-asynciterable-interface

%Symbol.asyncIterator% – a function that returns an AsyncIterator
object – The returned object must conform to the AsyncIterator
interface.

To make it short, to iterate over mysql.tables() like this…

for await (const it of mysql.tables()) {/*...*/}

You .tables() must return an iterable object, thus to make your code working you need to use AsyncIterable instead of AsyncIterator:

interface IDriver {
  tables(): AsyncIterable<Table>;
}

/*...*/

async *tables(): AsyncIterable<Table> {/*...*/}

TS Playground DEMO

interface Table {
  name: string;
  rows: number;
  type: string;
}

interface IDriver {
  tables(): AsyncIterable<Table>;
}

class MySql implements IDriver {
  
  private schema: string = 'myschema';    
  private async query<T>(sql: string): Promise<T[]> {        
    return [
      { table_name: 'table1', table_type: '***', table_rows: 10 },
      { table_name: 'table2', table_type: '***', table_rows: 20 },
    ] as unknown as T[];
  }  
 
  async *tables(): AsyncIterable<Table> {
    const sql = `select table_name, table_type, table_rows 
                 from information_schema.tables 
                 where table_schema = '${this.schema}' 
                 order by table_name;`;

    const rows = await this.query<{
      table_name: string;
      table_type: string;
      table_rows: number;
    }>(sql);
    
    for (const row of rows) {
      yield {
        name: row.table_name,
        rows: row.table_rows,
        type: row.table_type,
      } as Table;
    }
  }
}

async function run() {
  const mysql = new MySql();
  
  for await (const table of mysql.tables()) {
    console.log(table);
  }
}

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật