Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Type is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor

I've written this function in TypeScript:

export class LoginService {

  async isLoggedIn(): boolean {
    const r = await this.http.get('http://localhost:3000/api/user/isLoggedIn').toPromise();
    return r.body;
  }
}

When I try to run the Angular 6 application, I'm getting this error message:

ERROR in src/app/login.service.ts(28,23): error TS1055: Type 'boolean' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value.

I've used async/await in other applications before without hitting this before.

UPDATE: The question I would like answered is: how do I get the "isLoggedIn" function to return a boolean?

like image 411
user275801 Avatar asked Oct 08 '18 01:10

user275801


People also ask

What is the return type of an async function?

Async methods can have the following return types: Task, for an async method that performs an operation but returns no value. Task<TResult>, for an async method that returns a value. void , for an event handler.

Does es5 support async?

TypeScript has supported the async / await keywords since version 1.7, which came out in November of 2015. The compiler transformed asynchronous functions to generator functions using yield .

How use async await JS?

async and await Inside an async function, you can use the await keyword before a call to a function that returns a promise. This makes the code wait at that point until the promise is settled, at which point the fulfilled value of the promise is treated as a return value, or the rejected value is thrown.


2 Answers

An async function can ONLY return a promise by definition - all async functions return promises. It can't return a boolean.

That's what TypeScript is telling you. The async function can return a promise that resolves to a boolean.

The value that you return inside your async function becomes the resolved value of the promise that the async function returns. So, the return type for your async function is a promise (that resolves to a boolean).

The caller of isLoggedIn() will have to either use .then() with it or await with it.

export class LoginService {

    async isLoggedIn(): Promise<any> {
      const r = await this.http.get('http://localhost:3000/api/user/isLoggedIn').toPromise();
      return r.body;
    }

}
like image 73
jfriend00 Avatar answered Oct 17 '22 04:10

jfriend00


If your endpoint /api/user/isLoggedIn returns only a boolean value, you should just be able to use below by casting the http get method. But indeed you can only return a promise from an async function.

export class LoginService {
  async isLoggedIn(): Promise<boolean> {
    return this.http.get<boolean>('http://localhost:3000/api/user/isLoggedIn').toPromise();
  }
}

You could have an async function that consumes isLoggedIn() like this:

async doSomething() {
  const loggedIn: boolean = await service.isLoggedIn();
  if (loggedIn) {
    doThis();
  }    
}

Which would be called like this

await doSomething();
like image 24
Ian Avatar answered Oct 17 '22 04:10

Ian