troubleshooting Question

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

Avatar of Walter Grimm
Walter Grimm asked on
JavaScript
13 Comments1 Solution148 ViewsLast Modified:
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;
             };  
            }
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 13 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 13 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros