Skip to content

Instantly share code, notes, and snippets.

@reddyonrails
Created July 20, 2018 23:29
Show Gist options
  • Select an option

  • Save reddyonrails/5b39d40a4753269465644053bf5e937a to your computer and use it in GitHub Desktop.

Select an option

Save reddyonrails/5b39d40a4753269465644053bf5e937a to your computer and use it in GitHub Desktop.

Revisions

  1. reddyonrails created this gist Jul 20, 2018.
    15 changes: 15 additions & 0 deletions async_generator.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    async function *getRecords() {
    const initialResponse = await getRecords();
    yield* initialResponse.data;

    let nextPage = initialResponse.pagination_token;
    while(nextPage) {
    const response = await getRecords(nextPage);
    yield* response.data;
    nextPage = response.pagination_token;
    }
    }

    for await (const record of getRecords()) {
    handle(record);
    }