Retrieve data from indexedDB and return value to function calling
<p>I have share variable between javascript function which is asynchronous. One of them is main thread and another is event based. I want to return value when event is completed. </p>

<p>This is the code:</p>

<pre><code>completeExecution = false;    // Shared Variable (Global Variable)
indexDBdata = {};             // Shared Variable (Global Variable)

function getPermission(key) {
    var permission_data={};

    if(exist_in_local) {
       indexdbConnection.getRecordByKey('userPermission',permitKey,function(data){
        indexDBdata=data; // Before its complete function return value
    });
    } else {
       // make ajax call &amp; its working fine
    }
    return permission_data;
}
</code></pre>

<p>//get Data from IndexedDB</p>

<pre><code>getRecordByKey:function(tableName,key,readRecords){
    if(isEmptyOrNull(readRecords)){
        console.log("callback function should not be empty");
        return;
    }

    if(isEmptyOrNull(tableName)){
        console.log("table name should not be empty");
        return;
    }

    var returnObj={};
    var isSuccessfull=false;

    if(this.dbObject.objectStoreNames.contains(tableName)){
        var transaction=this.dbObject.transaction(tableName);
        var objectStore = transaction.objectStore(tableName);
        objectStore.get(key).onsuccess = function(event) {
            returnObj=event.target.result;
        };

        **//Return object after this events compelte**
        transaction.oncomplete = function(evt) { 
            completeExecution=true;
            indexDBdata=returnObj;
            readRecords(returnObj);

        };

        transaction.onerror = function(evt) {
            completeExecution=true;
            indexDBdata={status:'404'};
            readRecords("Table Not found");
        };
    } else {
        completeExecution=true;
        indexDBdata={status:'404'};
        readRecords("Table Not found");
    }

}
</code></pre>

<p><strong>Problem</strong> is while retrieving data from indexedDB it always returns <code>{}</code> (empty object). I want to synchronised event thread and main thread or wait for event to be completed. I don't want to directly manipulate DOM on callbacks I have to return value. </p>

<p>If you have solution to above problem or any other trick then please help me.</p>

<p>Thanks in advance. </p>