First off – we are on untread territory here, so while it works in newest firefoxes, the doc on MDN isn’t ready during the time of writing. I’ll fix the MDN later (maybe, there’s a lot of places that need fixing), so I’ll provide a glossary.
I want to create a Iterator from a callback:
I have a class that is constructed using two callbacks as argument. Let’s call the instance “listener”. This listener then repeatedly calls the first callback with some argument until it is finished listening, then calls the second callback once.
I want to wrap an Iterator around this, which yields each argument that the listener called the first callback with, then throws StopIteration as soon as the second one is called.
Like this:
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 does stuff with it.
//listener has called finished, so magicIter has thrown StopIteration
//so the loop in exhaustIterator has stopped
Note that I’m doing all this in an Addon SDK addon, so i can use promises and related stuff. And don’t need lectures about how browsers don’t know anything what I’m trying to do ;)
/edit: if you ask why i don’t just convert everything to callback-based code have a taste and tell me how to convert that to callback-based code without crying bloody tears. I’ll just wrap my main function into something mentioned here.