首页 文章

是否可以通过使用“return”语句从JavaScript立即调用的箭头函数中获取值? [重复]

提问于
浏览
2

这个问题在这里已有答案:

在这个代码示例中:

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

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

1 回答

  • 6

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

    请尝试以下方法:

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

相关问题