Avatar of Walter Grimm
Walter Grimm

asked on 

Have function wait to return value with request.onsuccess in the function

I have a javascript codeblock for Indexeddb call, see below  

Link to sample: https://test.quality-auditors.com/IndexedDB/? 

I have found some sites on it, but i could not really identify how to solve it, maybe somebody can give me babysteps.

When I call this function, I recieve the reply immediatly, the function does not wait for the request.onsuccess to complete when finishing the function

So when i call this function, i cannot the recieve the final value, somehow I need to get the function to wait till the request.onsuccess has completed.  Can someone add just the lines of code so this would work.

I have added a callback , but this fires bevore the onsuccess routine calls the callback - it replies to the call in the line var request = objectStore.get(cursorkey);  

            alert('call at start: ' + getmyitem('a2d9592e-11c7-4d20-cccd-dd293988ce86'));

            function getmyitem(myuuid) {

                readitem(myuuid, function (dataset) {
                    alert('Data in getmyitem: ' + dataset);
                    return dataset
                });
            }

            function readitem(cursorkey, callback) {
            alert("Readdetails "+cursorkey);
                  var transaction = db.transaction(["note"]);  
                  var objectStore = transaction.objectStore("note");  
                  var request = objectStore.get(cursorkey);  
                request.onerror = function (event) {  
                callback('error');
                console.dir(event);
                  };  
                  request.onsuccess = function(event) {  
                var note = request.result;
                document.getElementById("noteDetail").innerHTML = "<h2>"+note.title+"</h2><p>"+note.body+"</p>";
                //$("#noteDetail").html("<h2>"+note.title+"</h2><p>"+note.body+"</p>");
                alert("Readdetails2 " + note.title);
                callback(note.title);
                //return note.title;
             };  
            }
JavaScript

Avatar of undefined
Last Comment
Walter Grimm

8/22/2022 - Mon