Skip to content

Commit 1b55d90

Browse files
Flarnacodebytere
authored andcommitted
test: AsyncLocalStorage works with thenables
This adds a test to verify that AsyncLocalStorage works with thenables. PR-URL: #34008 Refs: #33778 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Stephen Belanger <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 0dd67d9 commit 1b55d90

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
5+
const assert = require('assert');
6+
const { AsyncLocalStorage } = require('async_hooks');
7+
8+
// This test verifies that async local storage works with thenables
9+
10+
const store = new AsyncLocalStorage();
11+
const data = Symbol('verifier');
12+
13+
const then = common.mustCall((cb) => {
14+
assert.strictEqual(store.getStore(), data);
15+
setImmediate(cb);
16+
}, 4);
17+
18+
function thenable() {
19+
return {
20+
then
21+
};
22+
}
23+
24+
// Await a thenable
25+
store.run(data, async () => {
26+
assert.strictEqual(store.getStore(), data);
27+
await thenable();
28+
assert.strictEqual(store.getStore(), data);
29+
});
30+
31+
// Returning a thenable in an async function
32+
store.run(data, async () => {
33+
try {
34+
assert.strictEqual(store.getStore(), data);
35+
return thenable();
36+
} finally {
37+
assert.strictEqual(store.getStore(), data);
38+
}
39+
});
40+
41+
// Resolving a thenable
42+
store.run(data, () => {
43+
assert.strictEqual(store.getStore(), data);
44+
Promise.resolve(thenable());
45+
assert.strictEqual(store.getStore(), data);
46+
});
47+
48+
// Returning a thenable in a then handler
49+
store.run(data, () => {
50+
assert.strictEqual(store.getStore(), data);
51+
Promise.resolve().then(() => thenable());
52+
assert.strictEqual(store.getStore(), data);
53+
});

0 commit comments

Comments
 (0)