Open mweathers opened 6 years ago
I also observed the same problem, however it's not related to generators:
const ns = cls.createNamespace("namespace1");
ns.run(() => {
ns.set("name", "Nishant");
setTimeout(() => {
Promise.resolve()
.then(() => {
for (var i = 0; i < 10; i++) {
if (i === 8) {
console.log(ns.get("name"));
throw new Error("causing trouble");
}
}
}).catch(err => {
console.log("catching the errors");
});
});
}, 1000);
setTimeout(() => {
console.log(ns.get("name"));
}, 2000);
Expected output:
catching the errors
undefined
Actual output:
catching the errors
Nishant```
I tested it, but the issue did not occur. using node v14.16.1, ts 4.2.3
const ns = cls.getNamespace('app');
ns.run(async () => {
ns.set('key', 'value');
await Promise.resolve()
.then(() => {
console.log({ key1: ns.get('key') });
throw new Error('test');
})
.catch(e => {
console.log(e);
});
});
setTimeout(() => {
ns.runAndReturn(() => {
console.log({ key2: ns.get('key') });
});
}, 100);
await Bluebird.delay(1000);
output:
{ key1: 'value' }
Error: test
{ key2: 'value' }
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
JS:
Expected Output:
Actual Output: