Skip to content

Instantly share code, notes, and snippets.

@nolanlawson
Created January 24, 2018 23:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nolanlawson/776c250eb0f1723a83bd782fde55ef8f to your computer and use it in GitHub Desktop.
Save nolanlawson/776c250eb0f1723a83bd782fde55ef8f to your computer and use it in GitHub Desktop.
Test IndexedDB + microtasks #2
<!doctype html>
<html>
<head>
<title>Demo of IndexedDB + microtasks Test 2</title>
</head>
<body>
<h1>Demo of IndexedDB + microtasks Test 2</h1>
<pre id="display"></pre>
<script>
(function () {
function log(str) {
document.getElementById('display').textContent += str + '\n'
}
var openReq = indexedDB.open('mydb', 1)
openReq.onupgradeneeded = function (e) {
var db = e.target.result
var store = db.createObjectStore('keyvalue', {keyPath: 'key'})
store.put({key: 'foo', value: 'bar'})
}
openReq.onsuccess = function (e) {
var db = e.target.result
var tx = db.transaction('keyvalue', 'readwrite')
var store = tx.objectStore('keyvalue')
log('get()')
new Promise(function (resolve, reject) {
store.get('foo').onsuccess = function (e) {
log('get() onsuccess with result: ' + JSON.stringify(e.target.result))
resolve(e.target.result)
}
}).then(function (result) {
log('promise callback with result: ' + JSON.stringify(result))
log('put()')
var req
try {
req = store.put({key: 'foo', value: 'bar'})
} catch (e) {
log('put() error: ' + e)
}
req.onsuccess = function () {
log('put() onsuccess')
}
req.onerror = function () {
log('put() onerror')
}
})
tx.oncomplete = function () {
log('transaction: oncomplete')
}
tx.onerror = function (e) {
log('transaction: onerror: ' + e.target.error)
}
}
})()
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment