我想编写一个函数,使用jQuery插件检查IndexedDB中是否存在已定义的值。
我尝试使用它,但它不起作用,因为返回发现是在promise.done()完成之前执行的。我该如何解决这个问题?
非常感谢。
function equipmentHasOrder(equipment) {
var orderObjectStore = $.indexedDB("order").objectStore("openOrders");
var found = false;
var index = orderObjectStore.index("EQUIPMENT");
var promise = index.get(equipment);
promise.done(function (result, event) {
if(result !== undefined) {
found = true;
}
});
return found;
promise.fail(function (error, event) {
error; // Type of error that has occured
event; // Error event
event.type; // indicates if there was an error or an exception
});
答案 0 :(得分:1)
您可以将承诺链接到then
:
function equipmentHasOrder(equipment){
var orderObjectStore = $.indexedDB("order").objectStore("openOrders");
var index = orderObjectStore.index("EQUIPMENT");
var promise = index.get(equipment);
// note the `.then` for chaining and the fact we're returning it.
return promise.then(function (result) {
return (result !== undefined);
});
}
然后你就可以做到:
equipmentHasOrder(equipment).then(function(result){
if(result){ // found
} else { // handle not found
}
});