小编Shi*_*lly的帖子

如何摆脱函数中的异步?

假设我有这个代码:

const myFunction = async => {
  const result = await foobar()
}

const foobar = async () => {
  const result = {}
  result.foo = await foo()
  result.bar = await bar()
  return result
}
Run Code Online (Sandbox Code Playgroud)

我想要这个:

const myFunction = () => {
  const result = foobar()
}
Run Code Online (Sandbox Code Playgroud)

我尝试像这样包装foobar :

const foobar = async () => {
  return (async () => {
    const result = {}
    result.foo = await foo()
    result.bar = await bar()
    return result
  })()
}
Run Code Online (Sandbox Code Playgroud)

但这仍然返回一个承诺

我不能在 …

javascript async-await nightwatch.js

5
推荐指数
1
解决办法
1748
查看次数

标签 统计

async-await ×1

javascript ×1

nightwatch.js ×1