Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Nodejs / javascript return data from function as its available

I am working with an API that gives data back to me that is paginated. IE, I request a page size of 50, get some data back, and then use the ID of the last bit of data I received to make another request. I want to write a function that returns data as it receives it, is this creating a stream and sending data through it? What I’m trying to avoid is waiting for one function to finish running through ALL of the pages, using up lots of memory, and then returning a massive array so that the main function can then pass all of that to another function to start processing the data. Ideally as data is retrieved from my API function it will return it to main and main can start processing it right away. The purpose of this is to limit how much memory is used at once and limit execution time. Is this a reasonable way to structure this and how would I go about implementing it? Are there better ways to structure this that accomplish the same goals without shoving all of the code into one giant/messy function?

TIA

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This sounds like a good use-case for an AsyncGenerator. You can loop over an asyncgenerator and get multiple results, instead of 1 final return.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading