What if I do not mention the return statement in the async function?
What if I do not mention the return statement in the async function?
Table of contents
No headings in the article.
If you don't include a return statement in an async function, the function will still run without any errors. However, it will return an implicit value of undefined
.
In other words, if you try to await the async function, you will get a resolved Promise with the value undefined
.
For example, consider the following async function:
javascriptCopy codeasync function exampleAsyncFunction() {
console.log("This function doesn't have a return statement");
}
(async () => {
const result = await exampleAsyncFunction();
console.log(result); // Output: undefined
})();
In this example, exampleAsyncFunction
doesn't have a return statement, so it will implicitly return undefined
. When we await this function, we get a resolved Promise with the value undefined
, which we log to the console.
It's generally a good practice to include a return statement in your async functions to make the return value explicit and avoid confusion.