如何从事件处理程序中返回indexedDB查询结果?

编程入门 行业动态 更新时间:2024-10-28 12:18:17
本文介绍了如何从事件处理程序中返回indexedDB查询结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我必须从indexedDB返回查询结果,但结果仅在成功

I have to return query result from indexedDB, but the result is only available in onsuccess event handler.

1 function listPeople(){ ... 4 var open = indexedDB.open("AccordionDatabase",1), 5 res; 6 7 open.onsuccess = function(){ 8 var db = open.result; 9 var transaction = db.transaction("PeopleStore", "readwrite"); 10 var store = transaction.objectStore("PeopleStore"); 11 var request = store.getAll(); 12 request.onsuccess = function(event){ 13 res = event.target.result; 14 console.log(res); 15 }; 16 17 // Close the db when the transaction is done 18 transaction.oncomplete = function() { 19 db.close(); 20 }; 21 22 }; 23 return res; 24 }

函数调用的输出显示 undefined ,尽管控制台会打印结果数组。请指导我如何将变量用作输出。

The output of the function call shows undefined, though console prints the result array. Please guide me how to use the variable as output.

推荐答案

您可以使用以下项目: github/jakearchibald/idb

you can either use this project: github/jakearchibald/idb

将indexedDB操作包装到一个Promise中,从而使列表中的人员异步:

or you need to wrap indexedDB operation into a promise and thus make the list people asynchronous:

function listPeople() { // ... return new Promise(function (resolve, reject) { var open = indexedDB.open("AccordionDatabase",1), open.onsuccess = function() { var db = open.result; var transaction = db.transaction("PeopleStore", "readwrite"); var store = transaction.objectStore("PeopleStore"); var request = store.getAll(); request.onsuccess = function(event){ resolve(event.target.result); }; request.onerror = function(event) { reject(event) } // Close the db when the transaction is done transaction.oncomplete = function() { db.close(); }; transaction.onerror = function(event) { reject(event) } }; open.onerror = function(event) { reject(event) } }) } // usage: function doWorkWithListedPeople(people) { // ... } function handleErrorsDuringIndexedDbRequest(event) { // ... } listPeople().then(doWorkWithListedPeople).catch(handleErrorsDuringIndexedDbRequest) // or also listPeople().then(function(people) { doWorkWithListedPeople(people) })

问题是,您创建一个表示最终将完成的工作的承诺。您可以告诉JS,最终,当诺言得到解决(成功)后,您想要然后处理已传递给 resolve的任何内容。参见 developer.mozilla/cs/docs / Web / JavaScript / Reference / Global_Objects / Promise 和 developer.mozilla/zh-CN/docs/Web/JavaScript/Reference/Statements/async_function 了解详情

The thing is, you create a promise that represents work that will be eventually done. You can tell JS that eventually, when the promise has been resolved (success), you want to then to work with anything that has been passed to resolve. See developer.mozilla/cs/docs/Web/JavaScript/Reference/Global_Objects/Promise and developer.mozilla/en-US/docs/Web/JavaScript/Reference/Statements/async_function for details

编辑

此外,如果您不喜欢那么,则可以使用async await进行处理,从而取消了承诺。请注意,您只能在异步函数中使用await关键字:

furthermore, if you don't like thens, you can make do with async await, which unwraps promises. Just beware, you can only use await keyword from within async function:

async function doStuff() { try { var people = await listPeople() // now you can work with ppl } catch (err) { // handle listPeople errors here } }

更多推荐

如何从事件处理程序中返回indexedDB查询结果?

本文发布于:2023-11-11 13:38:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1578594.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:查询结果   事件   程序   indexedDB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!