微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

创建具有项目计数的新对象

如何解决创建具有项目计数的新对象

我想创建一个函数,它从几个来源获取参数,并返回一个带有新对象的承诺。 所以,我有一个包含所有数据的基础对象,还有另一个函数,它接受一个参数并进行一些操作并返回另一个数据。 主要问题是我有一些竞争条件或新对象没有按预期更改,可能是因为嵌套函数

我尝试查看其他答案,但没有成功。

介绍够了,这是我的一些代码

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 举报,一经查实,本站将立刻删除。