-
Notifications
You must be signed in to change notification settings - Fork 207
Open
Description
I've run into an issue when throwing an error from a promise chain (even if the error is caught and handled) that is wrapped in TypeScript's async/await generator. It seems as though new contexts inherit from the context that had the error thrown. I've written some example TS code that demonstrates this, as well as the JS output by the TS compiler.
TypeScript
const cls = require('continuation-local-storage');
const ns = cls.createNamespace('my ns');
ns.run(async () => {
ns.set('key', 'value');
await Promise.resolve().then(() => {
console.log({ key1: ns.get('key') });
throw new Error('test');
}).catch(() => {});
});
setTimeout(() => {
ns.runAndReturn(() => {
console.log({ key2: ns.get('key') });
});
}, 1000);
JS:
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
const cls = require('continuation-local-storage');
const ns = cls.createNamespace('my ns');
ns.run(() => __awaiter(this, void 0, void 0, function* () {
ns.set('key', 'value');
yield Promise.resolve().then(() => {
console.log({ key1: ns.get('key') });
throw new Error('test');
}).catch(() => { });
}));
setTimeout(() => {
ns.runAndReturn(() => {
console.log({ key2: ns.get('key') });
});
}, 1000);
Expected Output:
{ key1: 'value' }
{ key2: undefined }
Actual Output:
{ key1: 'value' }
{ key2: 'value' }
Metadata
Metadata
Assignees
Labels
No labels