views:

9

answers:

0

Hi all :)

I'm trying out IndexedDB, but I can't seem to make it work with generators, even though it was advised as one way to make IndexedDB easier to use.

The following code works (not using generators):

function loadItems (func_item) {
  var request = db.objectStore ("store").openCursor ();

  request.onsuccess = function (event) {
    var cursor = event.result;
    if (!cursor) {
     return;
    }

    func_item (cursor);
    cursor.continue ();
  };
};

So now it can be called like

loadItems (function (cursor) {
  // do something with each item
});

Trying the same with generators does not work:

function getItems () {
  var request = db.objectStore ("store").openCursor ();

  request.onsuccess = function (event) {
    var cursor = event.result;
    if (!cursor) {
     return;
    }

    yield cursor;
    cursor.continue ();
  };
};

var items = getItems ();
for (var cursor in items) {
 // do something with each item
}

The call to getItems () returns undefined, so the for loop is not executed. I guess the problem is that yield is called from within the onsuccess event handler, not from within getItems itself, so it doesn't turn into a generator.

But I can't figure out how it would work.

Any idea??