是否可以通过使用"return"语句从JavaScript立即调用的箭头函数中获取值?

eri*_*ric 2 javascript arrow-functions

在这个代码示例中:

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

代码有效,但返回的值无处可寻(不在其中result).有没有办法使用普通的return语句从这个函数中获取数据?

amr*_*ngh 6

由于您使用了异步函数,因此它返回promise而不是value.

请尝试以下方法:

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

result.then((res)=> console.log(res));
Run Code Online (Sandbox Code Playgroud)