I have this function:
this.get = function() {
var something = 0;
window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.msIDBTransaction;
window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange || window.msIDBKeyRange;
if (!window.indexedDB) {
return;
}
var openRequest = window.indexedDB.open("abc", 1);
openRequest.onupgradeneeded = function (e) {
var thisDB = e.target.result;
if (!thisDB.objectStoreNames.contains("xyz")) {
thisDB.createObjectStore("xyz");
}
}
openRequest.onsuccess = function (e) {
var transaction = e.target.result.transaction(["xyz"], "readwrite");
var store = transaction.objectStore("xyz");
var request = store.openCursor(1);
request.onerror = function (e) {
}
request.onsuccess = function (e) {
something = e.target.result.value.size;
console.log(1);
}
}
openRequest.onerror = function (e) {
}
console.log(2);
return something;
};
As you can see, I expect that 'something' will be assigned with 'e.target.result.value.size' first, then will be returned.
However, indexeddb is asynchronous, so it will return 0 instead of 'e.target.result.value.size' (which means that the log will print out 2 first, then 1)
So my question is, how can I return value of 'e.target.result.value.size'