gpt4 book ai didi

javascript - 如何在像 forEach reduce 这样的 javascript 函数中使用 promise 函数?

转载 作者:行者123 更新时间:2023-11-30 11:52:46 26 4
gpt4 key购买 nike

我正在使用这样的 promise 函数:

// WORK
let res = {approveList: [], rejectList: [], errorId: rv.errorId, errorDesc: rv.errorDesc};
for (let i = 0; i < rv.copyDetailList.length; i ++) {
const item = rv.copyDetailList[i];
const v = await convertCommonInfo(item);
if (!item.errorId) {
res.approveList.push(v);
} else {
res.rejectList.push(merge(v, {errorId: item.errorId, errorDesc: item.errorMsg}));
}
}

这很好用,但我想尝试使用一些功能函数,我发现我必须使用 map 然后 reduce

// WORK, But with two traversal
const dataList = await promise.all(rv.copyDetailList.map((item) => convertCommonInfo(item)));

const res = dataList.reduce((obj, v, i) => {
const item = rv.copyDetailList[i];
if (!item.errorId) {
obj.approveList.push(v);
} else {
obj.rejectList.push(merge(v, {errorId: item.errorId, errorDesc: item.errorMsg}));
}

return obj;
}, {approveList: [], rejectList: [], errorId: rv.errorId, errorDesc: rv.errorDesc});

我发现 forEach 函数不能工作:

// NOT WORK, not wait async function
rv.copyDetailList.forEach(async function(item) {
const v = await convertCommonInfo(item);
if (!item.errorId) {
res.approveList.push(v);
} else {
res.rejectList.push(merge(v, {errorId: item.errorId, errorDesc: item.errorMsg}));
}
});

这不起作用,它只是返回初始值。 事实上这让我很困惑,因为我等待函数,为什么不工作?

甚至我想使用reduce 函数:

// NOT WORK, Typescirpt can not compile
rv.copyDetailList.reduce(async function(prev, item) {
const v = await convertCommonInfo(item);
if (!item.errorId) {
prev.approveList.push(v);
} else {
prev.rejectList.push(merge(v, {errorId: item.errorId, errorDesc: item.errorMsg}));
}
}, res);

但是因为我使用的是 Typescript,所以我得到了这样的错误:

error TS2345: Argument of type '(prev: { approveList: any[]; rejectList: any[]; errorId: string; errorDesc: string; }, item: Resp...' is not assignable to parameter of type '(previousValue: { approveList: any[]; rejectList: any[]; errorId: string; errorDesc: string; }, c...'.
Type 'Promise<void>' is not assignable to type '{ approveList: any[]; rejectList: any[]; errorId: string; errorDesc: string; }'.
Property 'approveList' is missing in type 'Promise<void>'.

所以我想知道两件事:

  1. 为什么 forEach await 不能工作?
  2. 我可以在 reduce 中使用 promise 函数吗?

最佳答案

您可能正在倒退。通常,您希望使用 compose 组合所有转换并将该组合函数传递给 promise 的 .then 处理程序:

// assumes curried times and add functions
let tranformData = _.compose(square, times(2), add(3));

let foo = fetchNumberAsync() // returns a promise of 3
.then(transformData)
.catch(err => doSomethingWithError(err));

foo.then(n => console.log(n)); // prints 144 to the console ((3 + 3) * 2) ** 2

与备选方案比较:

// clear but wasteful
let foo = fetchNumberAsync()
.then(n => n + 3)
.then(n => n * 2)
.then(n => n ** 2)

// performant but opaque
let foo = fetchNumberAsync().then(n => ((n + 3) * 2) ** 2)

使用 compose(尤其是 memoize)是一个很好的中间道路。

关于javascript - 如何在像 forEach reduce 这样的 javascript 函数中使用 promise 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38968452/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com