gpt4 book ai didi

javascript - 我不能将静态值添加到 native javascript Promise 中吗?

转载 作者:搜寻专家 更新时间:2023-11-01 00:36:32 25 4
gpt4 key购买 nike

我正在遍历对象数组以构建我想要并行等待的 Promise 集合。我需要一个特定的属性(与 promise 无关)来保留结果,但我不能只将它添加为 Promise 的属性,当 promise 解析时它会被删除:

let arrayOPromises = someArrayOfValues.map((promiseParams) => {
let response = someFunctionThatReturnsAPromise(promiseParams);
response.valueINeedToPersist = promiseParams.objectPropertyINeed; //unique to each iteration of map()
return response;
});

await Promise.all(arrayOPromises);

// gives me the resolved promises, but not the added value

// [resolvedPromise1, resolvedPromise2];

// resolvedPromise1.valueINeedToPersist === 'undefined'

最佳答案

I need a specific property (unrelated to the promise) to persist in the result

是的,然后将其添加到结果中而不是添加到 promise 对象中:

const arrayOPromises = someArrayOfValues.map(async (promiseParams, objectPropertyINeed) => {
const response = await someFunctionThatReturnsAPromise(promiseParams);
// ^^^^^
response.valueINeedToPersist = objectPropertyINeed;
return response;
});

await Promise.all(arrayOPromises);

关于javascript - 我不能将静态值添加到 native javascript Promise 中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49763296/

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