How to loop through indexedDB tables synchronously?

html, indexeddb

Solution

The sync API is only available in a webworker. So this would be the first requirement. (As far as I know only IE10 supports this at the moment)

An other shot you can give is working with JS 1.7 and use the yield keyword. For more information about it look here

I would sugest to work with a callbakck method that you call when you reached the latest value.

function readData(callback){     
   var trans = '';         
   trans = idb.transaction(["tableName"],'readonly'); //Create the transaction   
   var request = trans.objectStore("tableName").openCursor();
   var maxKey;      
   request.onsuccess = function(e) {   
       var cursor = request.result || e.result;             
       if(cursor.value){             
          //logic to  and find maximum
          maxKey = cursor.primaryKey            
          cursor.continue();
       }
   } 
   trans.oncomplete = function(e) {
        callback(maxKey); 
   }
} 

Problem

I want to write a function in JS where I will loop through a tables in my indexed DB and get the maximum value of last modified of table and return that ``` function readData(){ var trans = ''; trans = idb.transaction(["tableName"],'readonly'); // Create the transaction var request = trans.objectStore("tableName").openCursor(); request.onsuccess = function(e) { var cursor = request.result || e.result; if(cursor) { // logic to and find maximum } else { return // max last modified } cursor.continue(); } } ``` IMP--Since onsuccess method is asynchronous how can i make it synchronous? so that my method readData() will return only when max last modified record is found successfully. I can call this method(readData()) synchronously to get last modified record of 2-3 tables if I want.

Original source