Created
January 24, 2018 23:34
-
-
Save nolanlawson/04e1a13863d10994dce1d5562b3dffed to your computer and use it in GitHub Desktop.
Test IndexedDB + microtasks #1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<title>Demo of IndexedDB + microtasks Test 1</title> | |
</head> | |
<body> | |
<h1>Demo of IndexedDB + microtasks Test 1</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()') | |
store.get('foo').onsuccess = function (e) { | |
log('get() onsuccess with result: ' + JSON.stringify(e.target.result)) | |
Promise.resolve().then(function () { | |
log('promise callback') | |
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