Async arrow function expected no return value

Async Arrow Function with No Return Value

An async arrow function is a function that is both asynchronous and written in the arrow function syntax.

When using the async arrow function, it is expected to return a Promise. However, if no explicit return value is provided inside the function, it is automatically treated as returning a Promise resolved with the value undefined.

Let’s see an example:

        const exampleFunction = async () => {
          console.log('Inside async arrow function');
        };
        
        exampleFunction().then(() => {
          console.log('Promise resolved'); 
        });
      

In this example, we define an async arrow function called exampleFunction. Inside the function, we only have a console.log statement. Since no explicit return value is provided, it implicitly returns a Promise resolved with the value undefined.

We then call exampleFunction() and attach a .then() method to the returned Promise. The .then() method will be executed once the Promise is resolved, and in this case, it will log ‘Promise resolved’ to the console.

So, even though the async arrow function doesn’t have an explicit return statement, it still returns a Promise which can be handled using .then() or await depending on the context.

Same cateogry post

Leave a comment