问题描述
我想创建一个函数,它从几个来源获取参数,并返回一个带有新对象的承诺。 所以,我有一个包含所有数据的基础对象,还有另一个函数,它接受一个参数并进行一些操作并返回另一个数据。 主要问题是我有一些竞争条件或新对象没有按预期更改,可能是因为嵌套函数。
我尝试查看其他答案,但没有成功。
介绍够了,这是我的一些代码:
imports...
const createNewObject = async () => {
try {
const baSEObjFromrequest = await myClient.request(query); // Returns array of objects
// this is the base object structure that returns from request:
baSEObjFromrequest = [
{data: "some data x",val: 1},{data: "some data y",val: 8},{data: "some data z",val: 7},{data: "some data x",val: 3},val: 11},]
// the pipeline should iterate over the array,with the converter function
// and return the info as the key of the new object
// which key is a counter of the values
newObject = {}
baSEObjFromrequestAbove.forEach(item => {
let keyFromConverter = converter(item.data);
newObject[keyFromConverter] += item.val;
})
// Now I want to return new object (as promise)
// in this structure,but somehow it breaks any time and not return it as well.
newObject = {
info_from_x: 4,// The value is the count of
info_from_y: 8,info_from_z: 18,}
return newObject; // As promise
} catch (error) {
console.error(err);
}
}
// This function do a call and convert the data to new info data
const converter = async (data) => {
let query = { "info": data }
try {
const resp = await axios.post(URL + '/info',query);
const data = await resp.data;
return data;
} catch (err) {
console.error(err);
}
};
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)