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?