how to make synchronous call to indexeddb method from javascript

前端 未结 1 1483
别那么骄傲
别那么骄傲 2021-01-20 05:03

I have one method say method1 in java script that is having another method say method2 call. method2 returns one value whi

相关标签:
1条回答
  • 2021-01-20 05:30

    Don't have method2 return a value. Instead, pass a callback function to method2 which will execute once the value has been fetched:

    function method2(firstData, completionCallback) {
    
        var dataArray = new Array();
        $.indexedDB("SelfAssessment").objectStore("First_Sheet", "readonly").each(function(elem) {
            dataArray.push(elem.value);
        }).done(function() { completionCallback(dataArray) });
    }
    

    And invoke it like this:

    function method1(){
        method2(userObj.first, function(dataArray) {
            // inside a function that is called by method2 when the value is fetched
            alert("dataArray size -"+dataArray.length);
    
            // do everything with dataArray inside this function
        }); 
    }
    
    0 讨论(0)
提交回复
热议问题