Why does TSlint still says "The return type of an async function or method must be the global Promise type"?
I can't understand what's wrong.
Why does TSlint still says "The return type of an async function or method must be the global Promise type"?
I can't understand what's wrong.
Try returning a Promise
-wrapped value corresponding to the expected generic type of the Promise
, something like so:
@Action
public async register(registerInfo: Account): Promise<boolean> {
const res = await http.post('users/', registerInfo);
return new Promise<boolean>((resolve, reject) => {
resolve(res.data.success);
// Or reject() if something wrong happened
});
// Or simply return a resolved Promise
return Promise.resolve(res.data.success);
}
Actually, you should also be able to then()
the result:
@Action
public async register(registerInfo: Account): Promise<boolean> {
return await http
.post('users/', registerInfo)
.then(res => res.data.success);
}
I think the problem is that you are trying to return the result of await instead of capturing the result and processing it:
@Action
public async register(registerInfo: Account): Promise<boolean> {
const result = await http.post('users/', registerInfo);
return result.data.success;
}
The method http.post
returns Observable
type, you can convert it to Promise using toPromise()
method.
Like - http.post.toPromise()
there can be 3 ways.
You can use generic type to avoid this.
you can check the success key before returning whether its boolean or not.
As you know the result from api response will be containing result.data.success as true/false boolean value but the typescript doesn't know that(thats why there is error while showing in typescript code) so you need to explicitly define the interface maybe for your api response, there you can make the success property as boolean.
© 2022 - 2024 — McMap. All rights reserved.
!!
in the return beforeres...
and let me know if the error is still there – Australasiaresult.data.success
is indeed of boolean type and not enquoted boolean which makes it a string, like "true" instead oftrue
. Can you please check in the Network tab of the browser console, or put a breakpoint on the method? – Cnsfalse | Promise<boolean>
. The function continued to work as before. – Kila