node-continuation-local-storage icon indicating copy to clipboard operation
node-continuation-local-storage copied to clipboard

TypeScript ES6 async/await generator and an error thrown in a native Promise

Open mweathers opened this issue 7 years ago • 2 comments

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

mweathers avatar Mar 19 '18 20:03 mweathers

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

nishch avatar Mar 28 '18 09:03 nishch

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

0Ams avatar Apr 16 '21 06:04 0Ams