1

I was trying to execute multiple tasks asynchronously in node.js then save the return value into an object while putting all the tasks(promises) in a promise.all([]) but it doesn't seem to be working.

//A really minified function for convienince which sends a req to an api then
//assigns the response to the object that is passed in as a parameter
async function getAssessmentData(cookie, params,refObj){
  const result = await fetch(
    `https://www.sampleURL.com?` +
      new URLSearchParams({
        academicYearId: params.academic_year,
        semesterId: params.semester,
        courseId: params.course,
      }),
  );
  //Assign the response to the object inside the parameter which i should have reference of
  refObj = await result.json();
     }
//I stringified the output object to display it here (consider it as a Javascript object) 
let output= [
    {
        "meta":{
           "academic_year":"2020/21",
           "year":"Year III",
           "semester":"One"
        },
        "assessment":[
           {
              "course_grade":"A",
              "course_link_data":{
                 "academic_year":"f4c351be-8ed7-4197-9768-25d6e67c063e",
                 "semester":"11111111-1111-1111-1111-111111111111",
                 "course":"73adfaa1-0666-46a5-a2c4-3b0970d9025d"
              },
              "course_assessment":{
                 "assessments":[
                    
                 ],
                 "total":null
              }
           },
           {
              "course_grade":"B",
              "course_link_data":{
                 "academic_year":"f4c351be-8ed7-4197-9768-25d6e67c063e",
                 "semester":"11111111-1111-1111-1111-111111111111",
                 "course":"6ab5fbe9-086e-46c8-b115-d0d9f19a98a3"
              },
              "course_assessment":{
                 "assessments":[
                    
                 ],
                 "total":null
              }
           }
        ],
        "footer":{
           "sgp":"0",
           "sgpa":"0",
           "cgp":"0",
           "cgpa":"0",
           "academicstatus":"Not determined yet!"
        }
     },
     {
        "meta":{
           "academic_year":"2020/21",
           "year":"Year III",
           "semester":"One"
        },
        "assessment":[
           {
              "course_grade":"A",
              "course_link_data":{
                 "academic_year":"f4c351be-8ed7-4197-9768-25d6e67c063e",
                 "semester":"11111111-1111-1111-1111-111111111111",
                 "course":"73adfaa1-0666-46a5-a2c4-3b0970d9025d"
              },
              "course_assessment":{
                 "assessments":[
                    
                 ],
                 "total":null
              }
           },
           {
              "course_grade":"B",
              "course_link_data":{
                 "academic_year":"f4c351be-8ed7-4197-9768-25d6e67c063e",
                 "semester":"11111111-1111-1111-1111-111111111111",
                 "course":"6ab5fbe9-086e-46c8-b115-d0d9f19a98a3"
              },
              "course_assessment":{
                 "assessments":[
                    
                 ],
                 "total":null
              }
           }
        ],
        "footer":{
           "sgp":"0",
           "sgpa":"0",
           "cgp":"0",
           "cgpa":"0",
           "academicstatus":"Not determined yet!"
        }
     }
 ]
promiseList = [];
  for (const element of output) {
      for (const elementNested of element.assessment) {
                promiseList.push(
getAssessmentData(identityCookie,elementNested.course_link_data,elementNested.course_assessment);
        )
          }   
      }
        await Promise.all(promiseList);
        console.log(JSON.stringify(output))

The course_link_data object is not changing. From some debugging, I've found out that the getAssesmentData function is working correctly but I expect the problem is that the elementNested.course_assessment object I'm passing to the function from the loop is not from the "output" object. since I'm going to send around 50 requests at once I thought I would do them asynchronously, is there a way to achieve this?

m.prime
  • 67
  • 7
  • 2
    `getAssessmentData` is not returning a promise with the data. Why are you doing it through `refObj` vs. returning the data asynchronously from `getAssessmentData`? – Joe Aug 31 '21 at 20:50
  • 1
    sorry, I must have made a mistake while pasting the code here. I will update the question accordingly. It's supposed to push a new promise object into the array – m.prime Aug 31 '21 at 21:02
  • 1
    "How to excute a task asynchronously in node.js?" the same way you do it in JavaScript. –  Aug 31 '21 at 21:12
  • "is there a way to achieve this?" you need to pass the index and child index of the output structure `output[ index ][ childIndex ] = yourChangedValueHere` otherwise you'll make a copy of the child `output` value by passing it to `getAssessmentData` since you unbind it by passing it to another function that doesn't access the output object – savageGoat Aug 31 '21 at 21:25
  • In other words; this function (note that the params have changed) `async function getAssessmentData(cookie, params, indexOrKey, childIndexOrKey)` should return `{newValues: value, indexOrKey, childIndexOrKey}` and once everything is fetched you update your output with the keys – savageGoat Aug 31 '21 at 21:33
  • "*Assign the response to the object inside the parameter which i should have reference of*" - no, that doesn't work. JavaScript does not pass arguments by reference. You only assign to the local `refObj` variable. Just `return` the value instead! – Bergi Aug 31 '21 at 23:40
  • Remove the `new Promise((resolve,reject)=>{` stuff. `getAssessmentData` already returns a promise, which you can directly put into your array. – Bergi Aug 31 '21 at 23:42
  • @savageGoat thankyou it works when I pass the original "output" object then also pass the indexes from the function then modify the "output" object. – m.prime Aug 31 '21 at 23:57
  • @bergi I've updated the question accordingly but I think the objects and arrays are passed by reference according to the comment that worked for me and this https://stackoverflow.com/questions/13104494/does-javascript-pass-by-reference – m.prime Aug 31 '21 at 23:59

0 Answers0