简体   繁体   中英

WebStorm: cannot find name 'await' on certain .ts files

I am encountering a weird issue with webstorm on certain files. I am receiving "TS2304: Cannot find name 'await'" and "TS1005: ',' expected". This is only occurring on two of several files using await/async syntax. I have tried to invalidate cache but this did not work. I also tried to reboot the application and the computer. Nothing has worked so far. Also, it seems to not have a problem with async just not await.

Here is a sample of one of the method declarations that I am getting this error from :

private async findUserByEmailAddress = (emailAddress): Promise<IGroupMemberModel> => {

    const user: IUserModel = await this.userRepository.findUserByEmailAddress(emailAddress);

    if (! user)
        Promise.reject(new NotFoundError("The specified user could not be found."))

    return user;
}

Could someone provide insight on what the cause of this issue might be?

This is not an IDE issue, it's a syntax issue. The async keyword should be placed before the parameter list of the arrow function:

private findUserByEmailAddress = async (emailAddress): Promise<IGroupMemberModel> => { 
    ... 
}

Edit

This is the full working sample based on the code, with added missing types:

interface IGroupMemberModel { }
interface IUserModel { }
class NotFoundError extends Error { }
class x {
    userRepository: {
        findUserByEmailAddress(emailAddress: any): Promise<IUserModel>;
    }
    private findUserByEmailAddress = async (emailAddress): Promise<IGroupMemberModel> => {

        const user: IUserModel = await this.userRepository.findUserByEmailAddress(emailAddress);

        if (!user)
            throw new NotFoundError("The specified user could not be found.");

        return user;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM