※ 引述《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
};
});
}