※ 引述《art1 (人,原来不是人)》之铭言:
43
: function openDB (name, version) {
: let request = indexedDB.open(name, version);
: request.onerror = (event) => {
: console.log('error');
: };
: request.onsuccess = (event) => {
: console.log('success');
: };
: request.onupgradeneeded = (event) => {
: console.log('upgrade');
: };
: }
通常遇到这种 evet driven 以及异步的,
我会把它全部包成 promise 处理,避免遇到你说的 callback 问题
function openDB(name, version) {
return new Promise((resolve, reject) => {
let request = indexedDB.open(name, version);
request.onerror = event => {
reject(do_something_stuff);
};
request.onsuccess = event => {
resolve(do_something_stuff);
};
request.onupgradeneeded = event => {
// do_something_stuff
};
});
}