猛跑小猪
1.回调函数function select(key,store, callback) { var request3 = store.get(key);
request3.onerror = function(event) { console.log("Error : " + event.errorCode);
};
request3.onsuccess = function(event) {
callback(event.target.result);
};
}// 调用select(key,store, function(result) { console.log(result)
})2.Promisefunction select(key,store, onsuccess) { var request3 = store.get(key);
return new Promise(function(resolve, reject) {
request3.onerror = function(event) {
reject(event);
};
request3.onsuccess = function(event) {
resolve(event.target.result);
};
})
}// 调用select(key,store).then(function(result){ // success
console.log(result)
}, function(e){ // error
console.log(e)
})3.Event// 监听自定义事件window.addEventListener('selectSuccess', function(e){ console.log(e.result)
})function select(key,store, onsuccess) { var request3 = store.get(key);
request3.onerror = function(event) { console.log("Error : " + event.errorCode);
};
request3.onsuccess = function(event) { // 触发事件
var myEvent = new Event('selectSuccess');
myEvent.result = event.target.result; window.dispatchEvent(myEvent);
};
}