同一类型字段的解析器将并行执行,因此
type
无法知道解析程序的用途
bkFrom
已决定。我认为处理这个问题的最好方法是将逻辑向上移动一级,进入解析器
getTansactions
.
getTransactions: async () {
// Get the transactions first
const transactions = await someCallToGetTransactions()
// Grab all our additional calls and use Promise.all to call them concurrently
const promises = transactions.reduce((memo, t) => {
memo.push(t.getTo())
memo.push(t.getFrom())
return memo
}, [])
const toFrom = await Promise.all(promises)
// Merge the results into one array
return transactions.map((t, index) => {
const bkTo = toFrom[index * 2]
const bkFrom = toFrom[(index * 2) + 1]
const type = getType(bkTo, bkFrom) //calculate type from your other fields
return Object.assign({}, t, { bkTo, bkFrom, type })
})
}
或者,您可以返回事务类的实例,并缓存
getTo()
和
getFrom()
这样:
class Transaction {
async getTo () {
if (!this._to) {
this._to = await //whatever
}
return this._to
}
}
这样,第一次
getTo()
调用时,它将获取值并将其保存在内存中。对它的任何后续调用(对于同一实例)都只会从内存中返回值。