Is there a better way to iterate over the results of a generator, my for loop is ugly:
for(let job = readyJob.next(); !job.done; job = readyJob.next()){ }
In context there is a generator function that can determine if there is a batch of work, consisting of 1..* jobs (the generator may also return no jobs in the batch). There is a continuous loop that instantiates the generator and iterates over the batch doing work on the job (logging).
Is there a more elegant solution to this iteration problem. I mean this looks like a traditional iterator from Java/C# which isn't bad. Something like an "each" would be super readable... That's my hope anyhow.
let getReadyJob = function *(instance){
let numJobs = 7 ; // getRandomInt(0, 10) ;
for(let i = 0; i < numJobs; i++) {
yield {
jobId: '' + instance + '::' + i,
jobReadyOn: (new Date()).valueOf()
};
}
}
then
while(true){
let readyJob = getReadyJob()
for(let job = readyJob.next(); !job.done; job = readyJob.next()){
console.log(JSON.stringify(job.value)) ;
}
}
Yes, if your environment already supports for...of:
for (var job of readyJob) {
// ...
}
If not, have seen this a couple of times:
var next;
while (!(next = readyJob.next()).done) {
var job = next.value;
// ...
}