I've developed a client library that exposes a method called iterator()
. This method returns a Promise instance created using require('promise')
library, which is completed with an iterator object.
This object contains a method called next()
which returns a Promise which is completed with a complex object like this: {done: [true|false], key: _, value: _}
Although iterator()
might pre-fetch some elements, next()
needs to return a Promise in case it results in a remote call.
Now, say a user wants to iterate over all elements until the Promise returned by next()
returns an object containing done: true
.
I've managed to achieve this using the following recursive method (I originally found this solution in this answer):
var iterate = client.iterator();
iterateTeams.then(function(it) {
function loop(promise, fn) {
// Simple recursive loop over iterator's next() call
return promise.then(fn).then(function (entry) {
return !entry.done ? loop(it.next(), fn) : entry;
});
}
return loop(it.next(), function (entry) {
console.log('entry is: ' + entry);
return entry;
});
});
The question is, would it be possible, using require('promise')
library, to build a non-recursive solution? The reason I'm interested in a non-recursive method is to avoid blowing up if the number of entries to iterate over is too big.
Cheers, Galder