promises.tap.js
3.72 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
'use strict';
var tap = require('tap')
, test = tap.test
, createNamespace = require('../context.js').createNamespace
;
test("continuation-local state with promises", function (t) {
t.plan(4);
var namespace = createNamespace('namespace');
namespace.run(function () {
namespace.set('test', 0xabad1dea);
t.test("chained promises", function (t) {
if (!global.Promise) return t.end();
namespace.run(function () {
namespace.set('test', 31337);
t.equal(namespace.get('test'), 31337, "state has been mutated");
Promise.resolve()
.then(function () {
t.equal(namespace.get('test'), 31337,
"mutated state has persisted to first continuation");
})
.then(function () {
t.equal(namespace.get('test'), 31337,
"mutated state has persisted to second continuation");
})
.then(function () {
t.equal(namespace.get('test'), 31337,
"mutated state has persisted to third continuation");
t.end();
});
});
});
t.test("chained unwrapped promises", function (t) {
if (!global.Promise) return t.end();
namespace.run(function () {
namespace.set('test', 999);
t.equal(namespace.get('test'), 999, "state has been mutated");
Promise.resolve()
.then(function () {
t.equal(namespace.get('test'), 999,
"mutated state has persisted to first continuation");
return Promise.resolve();
})
.then(function () {
t.equal(namespace.get('test'), 999,
"mutated state has persisted to second continuation");
return Promise.resolve();
})
.then(function () {
t.equal(namespace.get('test'), 999,
"mutated state has persisted to third continuation");
t.end();
});
});
});
t.test("nested promises", function (t) {
if (!global.Promise) return t.end();
namespace.run(function () {
namespace.set('test', 54321);
t.equal(namespace.get('test'), 54321, "state has been mutated");
Promise.resolve()
.then(function () {
t.equal(namespace.get('test'), 54321,
"mutated state has persisted to first continuation");
Promise.resolve()
.then(function () {
t.equal(namespace.get('test'), 54321,
"mutated state has persisted to second continuation");
Promise.resolve()
.then(function () {
t.equal(namespace.get('test'), 54321,
"mutated state has persisted to third continuation");
t.end();
});
});
});
});
});
t.test("forked continuations", function (t) {
if (!global.Promise) return t.end();
namespace.run(function () {
namespace.set('test', 10101);
t.equal(namespace.get('test'), 10101, "state has been mutated");
var promise = Promise.resolve();
promise
.then(function () {
t.equal(namespace.get('test'), 10101,
"mutated state has persisted to first continuation");
});
promise
.then(function () {
t.equal(namespace.get('test'), 10101,
"mutated state has persisted to second continuation");
});
promise
.then(function () {
t.equal(namespace.get('test'), 10101,
"mutated state has persisted to third continuation");
t.end();
});
});
});
});
});