Initially, we are exploring uncharted territory here. Although it functions in the latest versions of Firefox, the documentation on MDN is not yet ready at the time of writing. I will address the MDN later on (maybe, as there are numerous areas needing attention), so I will provide a glossary.
The task at hand is to create an Iterator from a callback:
I have a class that takes two callbacks as arguments when constructed. Let's name this instance "listener". This listener continuously invokes the first callback with some argument until the listening is completed, then calls the second callback once.
My objective is to wrap an Iterator around this process, which yields each argument received by the listener from calling the first callback and stops throwing StopIteration as soon as the second one is called.
Here is what I aim for:
var magicIter = new MagicIter();
var listener = new Listener(magicIter.ready, magicIter.finished);
//on another thread, listener calls ready(1); ready(2); finished();
exhaustIterator(magicIter); //loops over magicIter and performs actions.
//listener has called finished, causing magicIter to throw StopIteration
//thus ending the loop in exhaustIterator
Keep in mind that I am executing all of this within an Addon SDK addon, allowing me to utilize promises and other related functionalities. And please spare me any discussions regarding how browsers do not comprehend my intentions ;)
/edit: In case you wonder why I don't just convert everything to callback-based code, have a look and advise me on how to perform this conversion without shedding tears. I will simply encapsulate my main function using the method mentioned here.