0

A tad related to this question, but different.

TSLint complains on this code because it needs a typedef:

  private async getTranslations() {
    // this.translations is a public variable used by the html
    this.translations = await this._languageService.getTranslations('Foo');
  }

I updated it to

private async getTranslations() : void { ... }

And this gives me the error:

type 'void' is not a valid async function return in ES5 because it does not refer to a pPromise-compatible constructor value

How can I get this right without removing the async keyword?

Hypenate
  • 1,907
  • 3
  • 22
  • 38

3 Answers3

2

Change return type to:

private async getTranslations(): Promise<void> {
  this.translations = await this._languageService.getTranslations('Foo');
}
Chris Pickford
  • 8,642
  • 5
  • 42
  • 73
2

type 'void' is not a valid async function return in ES5 because it does not refer to a pPromise-compatible constructor value

you are getting error , because when you use async/await then return type is going to be Promise wrapped object, so function return type should be Promise<returntype>, and in your case it will be

 private async getTranslations() : Promise<void> {
    // this.translations is a public variable used by the html
    this.translations = await this._languageService.getTranslations('Foo').toPromise();
  }

and if you are using angular then service mostly returns observable object , so you need to convert it in promise object by calling toPromise() function of call (if you dont put toPromise it will not fire request if you used httpClient in you service to get result.).

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
1

Return the Observable type

private async getTranslations() : Observable<any>{ ... }
Sachila Ranawaka
  • 39,756
  • 7
  • 56
  • 80