1

So my code is of the "let's use Generators to avoid Callback Hell" variety. I'm trying to have an accessor function wrap a generator that handles opening IndexedDB. I need to have the generator yield to the "callback", and then yield to the accessor function so the accessor function can return the created object which originated in the generator. Maybe this is a brainfart, but I'm kinda lost as to how to do this without polling on a boolean (such as idb_ready = false; and wait for idb_ready to be true before starting the while(gen.next()) business).

Code:

accessor = function() {
    var gen = function* () {
        var ret = {};
        var request = indexedDB.open("blah", 1, "description");
        request.onupgradeneeded = resume;

        var event = yield;

        ret.db_instance = event.target.result;

        yield ret;
    };

    function resume(val) { gen.next(val); }

    gen.next(); // start the generator

    // HERE is where I need to wait for the second yield
}

Is there a way to do this without polling? Thanks!

iv597
  • 191
  • 2
  • 5
  • No, generators do not produce a synchronous outcome. Only the "synchronous" code *in* the generator is [asynchronously executed step-by-step](http://stackoverflow.com/q/23551418/1048572) (when an appropriate function is used for that, like the `co` library or whatever) – Bergi Jul 15 '14 at 21:32
  • Promises might be more what you're looking for. Handy for chaining asynchronous events: https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/Promise.jsm/Promise – alexp Aug 22 '14 at 16:58

0 Answers0