nodejs/test/parallel/test-async-local-storage-enter-with.js
Chengzhong Wu 8e7ae60e3b
async_hooks: enable AsyncLocalStorage once constructed
This fixes the leak behavior when using `enterWith` when no
`AsyncLocalStorage`s were enabled inside a promise.

PR-URL: https://github.com/nodejs/node/pull/58029
Fixes: https://github.com/nodejs/node/issues/53037
Refs: https://github.com/nodejs/node/pull/58019
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2025-04-27 21:43:28 +00:00

38 lines
878 B
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const { AsyncLocalStorage } = require('async_hooks');
// Verify that `enterWith()` does not leak the store to the parent context in a promise.
const als = new AsyncLocalStorage();
async function asyncFunctionAfterAwait() {
await 0;
als.enterWith('after await');
}
function promiseThen() {
return Promise.resolve()
.then(() => {
als.enterWith('inside then');
});
}
async function asyncFunctionBeforeAwait() {
als.enterWith('before await');
await 0;
}
async function main() {
await asyncFunctionAfterAwait();
await promiseThen();
assert.strictEqual(als.getStore(), undefined);
// This is a known limitation of the `enterWith` API.
await asyncFunctionBeforeAwait();
assert.strictEqual(als.getStore(), 'before await');
}
main().then(common.mustCall());