2

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?

eric
  • 511
  • 1
  • 4
  • 15

1 Answers1

6

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));
amrender singh
  • 7,949
  • 3
  • 22
  • 28
  • Yes, good point. I did also try a version with `var resulit = await (async...`, but this threw an exception. I'll play around with both the await and the .then approach a bit more. – eric Dec 10 '18 at 05:12
  • @eric You can't use await outside an async function, so you would need that whole statement to be inside a function declared `async` to use it. You can use Promise#then anywhere though. – Paul Dec 10 '18 at 07:43
  • Understood. The code you see is actually inside another `async` function; omitted here for brevity. I ultimately got it to work using await. This was one of those cases where I tried a dozen different things and nothing seemed to work. – eric Dec 11 '18 at 13:58
  • @eric Awesome to hear you were able to get it working :) – Paul Dec 11 '18 at 21:10