So I have an object with 'n' elements inside, I have to go inside each element and check the status with another function, the return of that function should give me how many elements are 'true' but outside of the forEach loop, the problem is that everything goes right inside the return of the 2nd function but outside prints [object promise] This is my code:
var count=0;
object_x = "relations": [
{
"rel": "System.LinkTypes.Related",
"url": "545",
"attributes": {
"isLocked": false,
"name": "Related"
}
},
{
"rel": "System.LinkTypes.Related",
"url": "494",
"attributes": {
"isLocked": false,
"name": "Related"
}
},
{
"rel": "System.LinkTypes.Parent",
"url": "508",
"attributes": {
"isLocked": false,
"name": "Parent"
}
}
],
object_x.forEach((element) =>{
var name = element['attributes];
name = name['name];
if(name=='Related'){
let return_of_function_A = function_A(element['url']); //Async function: based on "url" number this function will return true or false
return_of_function_A.then(function(result){
if(result == true){
count++;
}else;
});
}else;
});
console.log(count); //prints [object Promise]
Im not expert on javascript but i think this might be related to the return_of_function_A.then(function(result){ ...
a.function_Ashould return true or false, the code then treats it as a promise. Is it supposed to be a promise that's resolved to either true or false? In that case, you have a race condition oncount.async/await?