Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.');
    }
  };
};
like image 907
Jiří Lechner Avatar asked Sep 06 '25 22:09

Jiří Lechner


1 Answers

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();
like image 109
Pointy Avatar answered Sep 08 '25 12:09

Pointy