How to collect an unknown number of async calls

I had a challenge the other day where I would request some data from a server. But the data was not all returned at once. But rather paginated. And I wouldn’t know beforehand how many pages I would need to process. So I couldn’t just pre-nest my callbacks.

That’s when I re-discovered promises.

Basically I can create a new promise for each page I need to call. Then use Promise.all to wait for all the requests to complete wherein I can process the array of results.

The code snippet to grab all the billable hours from a Freshbooks account then looks like this

By |2016-03-16T11:25:06+00:00March 16th, 2016|Uncategorized|0 Comments

About the Author:

Andrew Golightly is the lead web developer here at Golightly+. He is a passionate fullstack JavaScript developer. And creates native apps too using React Native. To balance his love for coding, he also works as a counsellor.

Leave A Comment