I am using plain javascript. I have 3 tasks to do for each element of an array. I have created promises for each element, where each one promises to do the tasks for each element. Now inside each one, I want to make 3 promises, one for each task.
processElement=processArrayElementFunction(matrix);
unique.forEach(function (number,index)
{
promises.push(new promiseToProcessElement(index,number,processElement,matrix));
});
Promise.all(promises).then((results) => {console.log(results);});
function promiseToProcessElement(id,num,callbackProcessElement,matrix)
{
return new Promise((resolve, reject) => {
resolve(callbackProcessElement(id, num,matrix););
});
}
function processArrayElementFunction(matrix)
{
return function(index, number)
{
var promises=[];
promises.push(new promiseTask(index,sumRC,matrix));
promises.push(new promiseTask(index,sumAround,matrix));
promises.push(new promiseTask(number,repetitions,matrix));
Promise.all(promises).then((results) => {
return results;
});
};
}
function promiseToProcessElement(id,num,callbackProcessElement,matrix)
{
return new Promise((resolve, reject) => {
resolve(callbackProcessElement(id, num,matrix););
});
}
function promiseTask(num,callbackTask,matrix)
{
return new Promise((resolve,reject)=>
{
resolve(callbackTask(num,matrix));
});
}
sumRC,sumAround,repetitions are just some functions that do the tasks. They are not important.
Now the var result=callbackProcessElement(id, num,matrix);
in the function promiseToProcessElement
is undefined
.
I think the problem is, because, the program demands this result, without the completion of the 3 tasks for each element. Is this true? And how can i Fix it?