Wednesday, 3 May 2017

Using async/await with a class mehod in javascript

Basically i'm trying to do this:

// UserProvider.js
module.exports = class UserProvider {
    async getUserAsync(username) {
        const res = await fetch('https://api.github.com/users/' + username);
        return { user: await res.json(), found: res.status === 200 };
    }
}

in index.js

let provider = new UserProvider();
const userResult = await provider.getUserAsync('muraterd');

But i'm getting this error:

const userResult = await provider.getUserAsync('muraterd');
                         ^^^^^^^^
SyntaxError: Unexpected identifier

When i try to call same method in the same class like below it works perfectly

const userResult = await this.getUserAsync('muraterd');



via Murat Erdogan

No comments:

Post a Comment