Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mweathers opened this issue Mar 19, 2018 · 2 comments

Comments

@mweathers
Copy link

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' }
@nishch
Copy link

nishch commented Mar 28, 2018

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

@0Ams
Copy link

0Ams commented Apr 16, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants