“等待返回”有什么区别吗?

Ven*_*sky 2 javascript async-await es6-promise

有什么区别吗

const foo = async () => {
  // some other code that uses await
  return await bar()
}
Run Code Online (Sandbox Code Playgroud)

const foo = async () => {
  // some other code that uses await
  return bar()
}
Run Code Online (Sandbox Code Playgroud)

bar返回 Promise 的函数在哪里。

这是await多余的还是有什么区别?

Que*_*tin 6

这是多余的。

它从 bar 返回的 Promise 中提取值,然后foo用它解析返回的 Promise。

如果直接返回bar的 Promise,则 返回的 Promise 会foo采用它,达到相同的效果。

  • 第一种方法不**等待** `bar()` 的结果。实际上,这确实是一样的。JavaScript 不会“等待”。 (3认同)