似乎有一些问题将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()?
一个可以await是非承诺的,这很好。
所有这些表达式都是有效的,不会导致任何错误:
await 5
await 'A'
await {}
await null
await undefined
Run Code Online (Sandbox Code Playgroud)
等待不承诺有任何可检测的影响吗?为了避免潜在的错误,应该注意哪些行为上的差异?是否有任何性能差异?
以下两行是完全相同还是理论上不同?
var x = 5
var x = await 5
Run Code Online (Sandbox Code Playgroud)
怎么样?有什么例子可以证明区别吗?
PS:根据TypeScript作者,有一个区别:
var x = await 5;与...不同var x = 5;;var x = await 5;将在下一个tern中分配x 5,其中asvar x = 5;将立即评估。