admin管理员组文章数量:1414167
So I ran into this a few times now and I've always just ignored it.
In TypeScript when I code:
async function someAsyncFunc(): void {
const prom = await somePromise();
}
It plains to me that async function should always have the return type of: Promise<T>
. So what it wants from me is:
async function someAsyncFunc(): Promise<void>
I know that, but this method does not return anything. Up to this point, I've just always given in, and used the type Promise<void>
but this will lead to bugs as TypeScript now thinks this function returns a promise, won't it?
Had an idea whilst writing this question and tested it.. turned out to be true. Check out my own answer in case your wondering :)
Error message so it will hopefully be index by google:
The return type of an async function or method must be the global Promise<T> type.
So I ran into this a few times now and I've always just ignored it.
In TypeScript when I code:
async function someAsyncFunc(): void {
const prom = await somePromise();
}
It plains to me that async function should always have the return type of: Promise<T>
. So what it wants from me is:
async function someAsyncFunc(): Promise<void>
I know that, but this method does not return anything. Up to this point, I've just always given in, and used the type Promise<void>
but this will lead to bugs as TypeScript now thinks this function returns a promise, won't it?
Had an idea whilst writing this question and tested it.. turned out to be true. Check out my own answer in case your wondering :)
Error message so it will hopefully be index by google:
The return type of an async function or method must be the global Promise<T> type.
Share
Improve this question
asked Mar 19, 2020 at 16:53
EliasElias
4,1413 gold badges27 silver badges52 bronze badges
2
- 3 It does return a promise. All async functions return a promise. The promise might wrap a return value. Otherwise (for a void returning async functions), the promise wraps nothing. It's a good thing that typescript thinks that it's a function that returns a promise, because that's exactly what it is. – spender Commented Mar 19, 2020 at 16:58
-
@spender Yes, I know that it is a promise internally. But I didn't know at this point that any
async
function implicitly returns one. – Elias Commented Mar 19, 2020 at 18:21
3 Answers
Reset to default 5An async function is consider an extension of the Promise paradigm.
For javascript/typescript know that the return of a specific function is asynchronous is because it return a Promise. This means that the return type of an ansynchronous function is always Promise. Then, you can wrap a value into that promise which can be void, number, string, another promise, etc.
From MDN:
An asynchronous function is a function which operates asynchronously via the event loop, using an implicit Promise to return its result.
More over:
An async function can contain an await expression that pauses the execution of the async function and waits for the passed Promise's resolution, and then resumes the async function's execution and returns the resolved value. Remember, the await keyword is only valid inside async functions.
Resuming: The aysnc/await in functions are syntax sugar to facilitate the code readability.
Whist writing this question the solution came to my mind. So now I'm answering this myself in case anyone else is wondering.
When you create an async function
JavaScript implicitly returns a Promise (which then will be void). Much like if you're writing a normal void function you don't have to put return;
at the end, it just does return.
async function test() {};
// Will return a resolved void promise
// the code snipped result only shows "{}" check it out in
// the browser console and you'll see
console.log(test());
Async functions will always return a promise. The result of your answer to your question should print Promise{<state>: "<finished>"}
instead of void. You need to await the async function to see the void
response
async function test() {};
console.log(test());
bees
async function test() {};
console.log(await test());
本文标签: javascriptReturn void from async funtionStack Overflow
版权声明:本文标题:javascript - Return `void` from `async` funtion - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744708926a2621010.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论