|
| 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