4

I have this piece of code:

export class Profile {

    private resource: Resource = new Resource();


    /**
     * Problem here
     */
    async initialize(): Promise<void> {
        console.log(this.resource);

        var html = await this.resource.fetch(true);

        const $profile = jQuery(html);

        console.log($profile.find("span.largetext"));


    }
}

If you can see the line console.log(this.resource), I get undefined. Can't async methods access the "this"?

I also tested with console.log(this), and it returns Profile { } in the web inspector.

Is there a way I can access this?

3
  • What this refers to depends on how the method is called. Commented Feb 8, 2016 at 14:29
  • @FelixKling What would it change exactly? Commented Feb 8, 2016 at 16:34
  • Well, the value of this. But I just realized that you said it would refer to a Profile instance. In that case this doesn't have a resource property for whatever reason. Commented Feb 8, 2016 at 16:36

1 Answer 1

3
class Profile {

    private resource: number = 1;


    /**
     * Problem here
     */
    async initialize(): Promise<void> {
        console.log(this.resource);
    }
}


let p = new Profile();
p.initialize();


let p = new Profile();
p.initialize();

I created this sample script which transpiles to

var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promise, generator) {
    return new Promise(function (resolve, reject) {
        generator = generator.call(thisArg, _arguments);
        function cast(value) { return value instanceof Promise && value.constructor === Promise ? value : new Promise(function (resolve) { resolve(value); }); }
        function onfulfill(value) { try { step("next", value); } catch (e) { reject(e); } }
        function onreject(value) { try { step("throw", value); } catch (e) { reject(e); } }
        function step(verb, value) {
            var result = generator[verb](value);
            result.done ? resolve(result.value) : cast(result.value).then(onfulfill, onreject);
        }
        step("next", void 0);
    });
};
class Profile {
    constructor() {
        this.resource = 1;
    }
    /**
     * Problem here
     */
    initialize() {
        return __awaiter(this, void 0, Promise, function* () {
            console.log(this.resource);
        });
    }
}
let p = new Profile();
p.initialize();
//# sourceMappingURL=main.js.map

and the output is

1

as expected. So the conclusion is that it's not about this keyword. It's about Resource class, I guess.

Sign up to request clarification or add additional context in comments.

1 Comment

hi @MartyIX, in es2017 inside the async method for me I am able to access console.log(this) and it is also displaying in the console but during debugging when I am manually typing 'this' in the console then it is showing as undefined Any suggestion or solution for this issue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.