I've been thinking about this a bit, and I can't seem to come up with a reasonable solution about how to accomplish this. The problem statement is simple - write a generator that will lazily paginate a remote data set. To simplify things, this is ideally what I'd like for the user of my library to see:
for (var user of users()) {
console.log(user);
}
I can't quite seem to get a generator working. I was thinking logic like this would work, but I can't figure out how to implement it.
function* users() {
while (canPaginate) {
yield* getNextPageOfUsers() // This will need to return an array of users from an http request
}
}
I'm sure that I'm thinking about something wrong here, but I can't seem to find any examples of someone using a generator like this (mostly people using them with static data or people doing something like async(function*(){...})
which isn't exactly what I'm looking to do). The important part here is that I want the end user to be able to consume the data as described above.
-Vince