提问人:Walid 提问时间:10/7/2023 最后编辑:Walid 更新时间:10/8/2023 访问量:36
indexeddb (await async) 在没有 .then() 的情况下需要
indexeddb (await async) Needed without .then()
问:
当我尝试将 Cordova 应用程序从 using 更改为 indexeddb 时 我为localStorage创建了类似的函数 在没有 then() 的情况下,无法在下面的函数中设置 await async... 可能吗?
在TESTING中,我们遇到了第一个警报不起作用(结果现在返回[object Promise]) 第二个是工作 我需要帮助才能在没有 .then() 的情况下返回承诺
谢谢
<!DOCTYPE html>
<html>
<head>
<script type = "text/javascript">
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){window.console.log("Your browser doesn't support a stable version of IndexedDB.");};
//----------
var db;
var Cnn=window.indexedDB.open("AP",1);
Cnn.onerror=function(event){console.log("error:");};
Cnn.onsuccess=function(event){db=Cnn.result;console.log("success:"+db);};
//----------
Cnn.onupgradeneeded=function(event){
const BasicData=[
{ID:"Colors",Val:"34495e|2980b9|98b4c7|d9e4e9"},
{ID:"Lang",Val:"en~English|es~Español|fr~Française|de~Deutsch|tr~Türkçe|ar~العربية"},
{ID:"L",Val:window.navigator.language.substring(0,2)}
];
db=event.target.result;
var objectStore=db.createObjectStore("BasicData",{keyPath:"ID"});
for(var i in BasicData){objectStore.add(BasicData[i]);};
}
//----------
function idbFunc(ty,tb,ky,vl){//ty: HasKey,Get,GetAll,Set,Remove,RemoveAll
let RW=(ty=='HasKey'||ty=='Get'||ty=='GetAll')?"readonly":"readwrite";
let transaction=db.transaction([tb],RW);
let objectStore=transaction.objectStore(tb);
return new Promise(function(resolve,reject){
if(ty=='HasKey'){
let Cn=objectStore.get(ky);
Cn.onerror=function(){console.log("Unable to read database!");reject(false);};
Cn.onsuccess=function(){if(Cn.result){resolve(true);}else{resolve(false);}};
}else if(ty=='Get'){
let Cn=objectStore.get(ky);
Cn.onerror=function(){console.log("Unable to read database!");reject(false);};
Cn.onsuccess=function(){console.log("Get:"+Cn.result.Val);resolve(Cn.result.Val);};
}else if(ty=='GetAll'){
var r=[];
objectStore.openCursor().onsuccess=function(event){
let cursor=event.target.result;
if(cursor){
r.push({ID:cursor.ID,Val:cursor.Val});
cursor.continue();
}else{
resolve(r.length);
};
};
}else if(ty=='Set'){
let Cn=objectStore.add({ID:ky,Val:vl});
Cn.onerror=function(){console.log("Unable to add!");reject(false);};
Cn.onsuccess=function(){console.log("Added.");resolve(true);};
}else if(ty=='Remove'){
let Cn=objectStore.delete(ky);
Cn.onerror=function(){console.log("Unable to add!");reject(false);};
Cn.onsuccess=function(){console.log(K+" erased successfully");resolve(true);};
}else if(ty=='RemoveAll'){
var Cn=objectStore.clear();
Cn.onerror=function(){console.log("Unable to add!");reject(false);};
Cn.onsuccess=function(){console.log("Basic data has been erased successfully");resolve(true);};
}
})
}
// ------- TESTING -----------
setTimeout(function(){
alert(idbFunc('Get',"BasicData",'Lang',''))
idbFunc('Get',"BasicData",'Lang','').then(result =>{alert(result)})
},601);
</script>
</head>
<body>
<h1>test</h1>
</body>
</html>
```
答: 暂无答案
评论