我正在使用indexedDB,但遇到了问题。问题是当我请求某些事务(例如添加或从数据库读取)时。当我处理事件[成功,onerror]并将变量的值/分配给变量时,它会保留该值,并且可以在该处理程序内访问,但在处理程序外该值不会更改。
let getValue = {};
function getAll_PROMISE(signedInUser){
return new Promise(function(resolve, reject){
getValue = index.getAll(signedInUser);
getValue.onsuccess = function(event){
if(event.target.result){
alert("event.target. got result " + event.target.result);
resolve(event.target.result);
}else{
alert("Event.target have not result");
reject(undefined);
}
}
getValue.onerror = function(event){
reject(event.target.errorCode);
}
});
}
(function(){
try {
includedDate.forEach(async function(eachDate){
await getAll_PROMISE(currentSignedInUser).then((arrayOfObject) => {
foundNothing = false;
alert("foundNothing... "+foundNothing + arrayOfObject); //foundNothing is false here
arrayOfObject.forEach(function(usernameKey){
if(serializeDate(eachDate) == usernameKey.dateValueSaved){
console.log("foundDate: " + usernameKey.dateValueSaved + "wechi" +usernameKey.wechi + " mahtem: " + usernameKey.mahtem);
}
});
}, (rejectReason) => {
alert("The promise didn't reply anything or it rejected! " + rejectReason);
});
});
}catch(err){
alert("Catching error inside catch block: " + err);
}
})();
使用Promise后,变量foundNothing不会保留其value事件!