• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4
5const assert = require('assert');
6const { AsyncLocalStorage } = require('async_hooks');
7
8// This test verifies that async local storage works with thenables
9
10const store = new AsyncLocalStorage();
11const data = Symbol('verifier');
12
13const then = common.mustCall((cb) => {
14  assert.strictEqual(store.getStore(), data);
15  setImmediate(cb);
16}, 4);
17
18function thenable() {
19  return {
20    then,
21  };
22}
23
24// Await a thenable
25store.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
32store.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
42store.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
49store.run(data, () => {
50  assert.strictEqual(store.getStore(), data);
51  Promise.resolve().then(() => thenable());
52  assert.strictEqual(store.getStore(), data);
53});
54