Search code examples
javascriptarrow-functions

Is it possible to get a value from a JavaScript immediately-invoked arrow function by using a "return" statement?


In this code example:

let result = (async (global) => {
  // other code here (includes await's; thus the async)
  return 123;
})(this);

The code works, but the return'ed value is nowhere to be found (not in result). Is there any way to use a normal return statement to get data out of this function?


Solution

  • Since you have used an async function it returns a promise instead of a value.

    Try the following:

    var result = (async (global) => {
      // other code here (includes await's; thus the async)
      return 123;
    })(this);
    
    result.then((res)=> console.log(res));