Jiří Lechner
Jiří Lechner

Reputation: 820

IndexedDB cursors

my question is pretty simple. I can't understand an example from the MDN article about iterating a result from an IndexedDB. Specifically, I can't see any loop used for iteration. There is no for/while/do cycle. Here is the example:

function displayData() {
  var transaction = db.transaction(['rushAlbumList'], "readonly");
  var objectStore = transaction.objectStore('rushAlbumList');

  objectStore.openCursor().onsuccess = function(event) {
    var cursor = event.target.result;
    if(cursor) {
       var listItem = document.createElement('li');
       listItem.innerHTML = cursor.value.albumTitle + ', ' + cursor.value.year;
       list.appendChild(listItem);  

       cursor.continue();
    } else {
       console.log('Entries all displayed.');
    }
  };
};

Upvotes: 4

Views: 1969

Answers (1)

Pointy
Pointy

Reputation: 413707

The "loop" happens implicitly. Each successful advance of the cursor results in a "success" event, which will trigger a call to the handler assigned to the "onsuccess" property of the request. Thus, the iteration happens because of this line:

cursor.continue();

Upvotes: 6

Related Questions