This repository was archived by the owner on Feb 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample-tx-wait.html
More file actions
63 lines (56 loc) · 1.45 KB
/
example-tx-wait.html
File metadata and controls
63 lines (56 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<!DOCTYPE html>
<title>IDBTransaction.waitUntil()</title>
<script src="polyfill.js"></script>
<script>
'use strict';
function sleep(ms) {
return new Promise(function(resolve) {
setTimeout(resolve, ms);
});
}
var open = indexedDB.open('db' + Date.now());
open.onupgradeneeded = function() {
var db = open.result;
var s = db.createObjectStore('squares');
for (var i = 0; i < 10; ++i) {
s.put(i * i, i);
}
};
open.onsuccess = function() {
var db = open.result;
console.log('-- creating tx --');
var tx = db.transaction('squares');
var s = tx.objectStore('squares');
console.log('state >> ' + tx.state);
(function() {
console.assert(tx.complete === tx.complete);
var rq = s.get(0);
console.assert(rq.ready === rq.ready);
}());
var rq, rq2, p2;
tx.waitUntil(
rq = s.get(3).ready
.then(function(result) {
console.log('tx state >> ' + tx.state);
console.log('result >> ' + result);
return sleep(10);
})
.then(function() {
console.log('tx state >> ' + tx.state);
rq2 = s.get(5);
p2 = rq2.ready;
return p2;
})
.then(function(result) {
console.assert(p2 === rq2.ready);
console.log('tx state >> ' + tx.state);
console.log('result >> ' + result);
return sleep(10);
})
).then(function() {
console.log('tx then, state >> ' + tx.state);
}, function() {
console.log('tx caught, state >> ' + tx.state);
});
};
</script>