相关疑难解决方法(0)

等待Promise.all()和多个等待之间的任何区别?

之间有什么区别:

const [result1, result2] = await Promise.all([task1(), task2()]);
Run Code Online (Sandbox Code Playgroud)

const t1 = task1();
const t2 = task2();

const result1 = await t1;
const result2 = await t2;
Run Code Online (Sandbox Code Playgroud)

const [t1, t2] = [task1(), task2()];
const [result1, result2] = [await t1, await t2];
Run Code Online (Sandbox Code Playgroud)

javascript async-await

127
推荐指数
4
解决办法
7万
查看次数

JavaScript数组.reduce与async/await

似乎有一些问题将async/await与.reduce()结合起来,如下所示:

const data = await bodies.reduce(async(accum, current, index) => {
  const methodName = methods[index]
  const method = this[methodName]
  if (methodName == 'foo') {
    current.cover = await this.store(current.cover, id)
    console.log(current)
    return {
      ...accum,
      ...current
    }
  }
  return {
    ...accum,
    ...method(current.data)
  }
}, {})
console.log(data)
Run Code Online (Sandbox Code Playgroud)

data对象被记录之前this.store完成...

我知道你可以使用Promise.all异步循环,但这适用于.reduce()

javascript reduce promise async-await ecmascript-next

43
推荐指数
4
解决办法
2万
查看次数

等待多个并发等待操作

如何更改以下代码,以便触发异步操作并同时运行?

const value1 = await getValue1Async();
const value2 = await getValue2Async();
// use both values
Run Code Online (Sandbox Code Playgroud)

我需要做这样的事情吗?

const p1 = getValue1Async();
const p2 = getValue2Async();
const value1 = await p1;
const value2 = await p2;
// use both values
Run Code Online (Sandbox Code Playgroud)

javascript promise async-await es2017

31
推荐指数
2
解决办法
3820
查看次数