You could have an asynchronous generator function that cycle through all users in an API like JSONPlaceholder. This data structure is theoretically infinite because you don't know how many users are included in this API (well, in fact you can, but let's pretend).
So you could build a generator to return a promise listing all users. With the for-await loop, you could search for a specific user. This is great because it reduces the amount of memory needed to store the users.
"use strict";asyncfunction*users(){letindex=1;while(true){constresponse=awaitfetch(`https://jsonplaceholder.typicode.com/users/${index}`);if(!response.ok){break;}constuser=awaitresponse.json();yielduser;index++;}}asyncfunctionmain(){forawait(constuserofusers()){if(user.email.includes("net")){console.log(`${user.name} is a .NET user... Just kidding :)`);break;}}}main();
In this example, I have only fetched 3 users before finding the one I'm interested in. Whereas with a simple array, you would have requested for the entire database. Then you would have cycled through all of them, only to find that you needed the third one, leaving thousands in memory for nothing. This might not be the best case to illustrate what I'm trying to explain but generator functions are best to describe infinite data structures that would normally overflow the memory, or in the best case slow down the app a lot.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
You could have an asynchronous generator function that cycle through all users in an API like JSONPlaceholder. This data structure is theoretically infinite because you don't know how many users are included in this API (well, in fact you can, but let's pretend).
So you could build a generator to return a promise listing all users. With the for-await loop, you could search for a specific user. This is great because it reduces the amount of memory needed to store the users.
In this example, I have only fetched 3 users before finding the one I'm interested in. Whereas with a simple array, you would have requested for the entire database. Then you would have cycled through all of them, only to find that you needed the third one, leaving thousands in memory for nothing. This might not be the best case to illustrate what I'm trying to explain but generator functions are best to describe infinite data structures that would normally overflow the memory, or in the best case slow down the app a lot.