Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Node Promise properties

I’m developing an importer, for each category i have to create the category and their sizes, for example in this input JSON:

const input_json = {
"categories": [
        {
            
            "title": "category title",
            "sizes": [
                {
                    
                    "title": "size title",
                    "slices": 8,
                    "flavors": 4,
                    
                },
                {
                    "id" : "",
                    "title": "size title 2",
                    "slices": 8,
                    "flavors": 4,
                    
                }
            ]
       }
   ]
}

but the problem is that sizes need to know the category id, so i’m trying to do it like this:

const prepareCategories = (body) => {
    let category_promises = []

    for (let i = 0; i < body.categories.length; i++) {
        const categoryBody = body.categories[i]

        let object_category = {
           ......
        } // set the object
        
        categoryPromise = nmCategorySvcV2.create(object_category) 
        
        categoryPromise.size = categoryBody.sizes

        category_promises.push(
            categoryPromise,
        )
    }
    
    return category_promises
}

......

          let category_promises = prepareCategories(input_json) // passing the input 

            Promise.all(category_promises).then((categories) => {
                console.log(categories)
                
            })

but when I see the result of Promise.all the sizes property is not displayed, only the properties of the category actually created.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

what am i doing wrong?

>Solution :

You are setting the size on the promise (which doesn’t have any meaning). Instead you need to wait for the promise to be resolved and then set the size on its result:

const categoryPromise = nmCategorySvcV2.create(object_category)
 .then(result => Object.assign(result, { size: categoryBody.sizes }))

(By the way you were missing a declaration for categoryPromise.)

The code could become a bit clearer using await syntax:

const prepareCategories = body => body.categories.map(async categoryBody => {
  const object_category = {
    //......
  } // set the object
  const categoryData = await nmCategorySvcV2.create(object_category)
  categoryData.size = categoryBody.sizes 
  return categoryData
})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading