只需尝试编写一个“selectAll”方法,该方法将 objectStore 名称作为参数并返回其包含的所有键/值对,而不是简单地在对象存储的每次迭代上运行回调。
本质上,在执行 indexedDB 事务时模仿同步行为。
var results = []
request.onsuccess = function(e) {
var result = e.target.result;
if (!result) {
//I could call successcallback here with JUST this 'row' of data
successCallback(result);
return;
}
//I could push the results into an array here, but I would need to 'wait' until all the onSuccess methods have fired before returning it.
results.push(result.value);
result.continue();
}
我正在使用 Angular,我认为 Promise 可能是答案。特别是, $q.all() 在这种情况下会有所帮助吗?