When Is It Enough?

The big question that everyone runs into at least once in their lives is what’s enough? In reality as human beings, nothing is ever enough. There is always another level that we can strive for. Once…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Iterating over Paginated Data

Modern Asynchronous JavaScript — by Faraz K. Kelhini (27 / 58)

👈 Creating an Asynchronous Generator | TOC | Wrapping Up 👉

One situation we want to use asynchronous iteration over synchronous is when working with web APIs that provide paginated data. By using an asynchronous iterator, we can seamlessly make multiple network requests and iterate over the results. For example, GitHub provides an API that allows us to retrieve commits for a repository. The response is in JSON format and contains the data for the last 30 commits of the repository. The API will also provide pagination link headers for the remaining commits.

Say we want to retrieve info for the last 90 commits of a particular GitHub repository. We can achieve that using an asynchronous iterator or, better yet, a generator. Let’s create an asynchronous generator and program it to handle the pagination:

Here, we’ve created two async functions, one of which is a generator. The generator function is responsible for retrieving the resource, parsing it as JSON, and sending the info of each commit to the generator’s caller.

In order to fetch the last 90 commits, not just 30, we put these tasks in a loop within the…

Add a comment

Related posts:

Are you working just to get paid?

Many young people think of a job only as a way of earning money. Well, not me. Getting a job is not easy. Keeping it is even harder. And it is not just about getting it in the first place. It should…

About Coinstore.com

We are excited to announce that FIO has teamed up with Coinstore to integrate FIO Send into the Coinstore exchange to make an already easy-to-use and intuitive exchange interface an even easier and…

Most Chronic Pain May Be Psychological

According to Dr. John Sarno, 95% of back pain is psychological. He has allegedly seen over 20,000 patients.