File
Blob: src/workerd/api/tests/als-test.js
| 1 | // Copyright (c) 2024 Cloudflare, Inc. |
| 2 | // Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | // https://opensource.org/licenses/Apache-2.0 |
| 4 | import { strictEqual } from 'node:assert'; |
| 5 | |
| 6 | import { AsyncLocalStorage } from 'node:async_hooks'; |
| 7 | |
| 8 | export const customthenable = { |
| 9 | // Test to ensure that async context is propagated into custom thenables. |
| 10 | async test() { |
| 11 | const als = new AsyncLocalStorage(); |
| 12 | const result = await als.run(123, async () => { |
| 13 | return await { |
| 14 | then(done) { |
| 15 | done(als.getStore()); |
| 16 | }, |
| 17 | }; |
| 18 | }); |
| 19 | strictEqual(result, 123); |
| 20 | |
| 21 | const result2 = await als.run(123, async () => { |
| 22 | return await new Promise((resolve) => |
| 23 | resolve({ |
| 24 | then(done) { |
| 25 | done(als.getStore()); |
| 26 | }, |
| 27 | }) |
| 28 | ); |
| 29 | }); |
| 30 | strictEqual(result2, 123); |
| 31 | }, |
| 32 | }; |