forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test-async-local-storage-thenable.js
53 lines (43 loc) · 1.19 KB
/
test-async-local-storage-thenable.js
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
'use strict';
const common = require('../common');
const assert = require('assert');
const { AsyncLocalStorage } = require('async_hooks');
// This test verifies that async local storage works with thenables
const store = new AsyncLocalStorage();
const data = Symbol('verifier');
const then = common.mustCall((cb) => {
assert.strictEqual(store.getStore(), data);
setImmediate(cb);
}, 4);
function thenable() {
return {
then
};
}
// Await a thenable
store.run(data, async () => {
assert.strictEqual(store.getStore(), data);
await thenable();
assert.strictEqual(store.getStore(), data);
});
// Returning a thenable in an async function
store.run(data, async () => {
try {
assert.strictEqual(store.getStore(), data);
return thenable();
} finally {
assert.strictEqual(store.getStore(), data);
}
});
// Resolving a thenable
store.run(data, () => {
assert.strictEqual(store.getStore(), data);
Promise.resolve(thenable());
assert.strictEqual(store.getStore(), data);
});
// Returning a thenable in a then handler
store.run(data, () => {
assert.strictEqual(store.getStore(), data);
Promise.resolve().then(() => thenable());
assert.strictEqual(store.getStore(), data);
});