Search code examples
javascriptnode.jstypescriptwebstorm

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

What might be the cause of this issue?


Solution

  • 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;
        }
    }