Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
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.
UPDATED:
–
–
–
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()
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.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.