Tsuna
Tsuna

Reputation: 2196

using async await in class to assign properties but returns null

I have a class with _code set to null from the start then making a request to an url to get the result.

Somehow I am after assigning the class's property code with the result still gives me null though.

What am I doing wrong with this?

class R {
    constructor() {
        this._code = null;
    }

    get code() {
        return this._code;
    }

    set code(value) {
        this._code = value;
    }

    async makingRequests(id) {
        await this.requestToGetCode(id);
        // this gives me null
        console.log(this.code, 'this.code in rquest');
    }

    async requestToGetCode(id) {
        await request(url, async (error, response, body) => {
            if (body !== 'found_no_results') {
                switch (response.statusCode) {
                    case 200:
                        this.code = await JSON.parse(body);
                        // this does give me the proper result though
                        console.log(this.code, 'this.code in requestToGetCode');
                        break;
                    case 404:
                        console.log('page not found');
                        break;
                    default:
                        break;
                }
            } else {
                console.log(body, id);
            }
        });
    }
}

thanks in advance for any help and suggestions.

Upvotes: 3

Views: 461

Answers (1)

Mathyn
Mathyn

Reputation: 2610

As mentioned in the comments the Request library does not return a promise and instead works with a callback. You could use a library like request-promise to work around this. However if you do not want to do this for some reason this answer might help you.

To be able to use async/await with the Request library you need to manually wrap the call in a Promise.

async requestToGetCode(id) {
    await new Promise((resolve, reject) => {
        request(url, (error, response, body) => {
            if (body !== 'found_no_results') {
                switch (response.statusCode) {
                    case 200:
                        this.code = JSON.parse(body);
                        // this does give me the proper result though
                        console.log(this.code, 'this.code in requestToGetCode');
                        resolve();
                        break;
                    case 404:
                        console.log('page not found');
                        reject('Not found');
                        break;
                    default:
                        // Reject all other cases
                        reject('Error');
                        break;
                }
            } else {
                // Reject as we do not receive the correct response
                console.log(body, id);
                reject('Error');
            }
        });
    });
}

Essentially we are creating a new Promise here which will do the request for us. In the request callback we then call resolve or reject depending on the result.

Upvotes: 1

Related Questions