I need to create a function that receives a Promise as a first parameter and an array of its parameters (if it has at least one) or null if none.
My function looks this way:
var executeMappingStep = (promiseStatus, myPromise, myPromiseParams) => {
return new Promise((resolve, reject) => {
//if the execution was success, there's no need to invoke myPromise again
if(promiseStatus.success == true)
return resolve(promiseStatus.previousResponseSaved);
if (myPromiseParams!= null) {
//how I resolve this logic is my doubt
myPromise.addArrayOfParameters(myPromiseParams);
}
myPromise().then(result => resolve(result)).catch(err => reject(err));
});
};
I was reading about the .bind() function which works for functions but not for promises according to this question. I also saw something about the .apply() function, but this doesn't seems to be what I need.
Edit
My function is invoked like myFirstPromise when there aren't parameters, and like mySecondPromise when I have at least one:
var executePromises = (myFirstPromise, mySecondPromisePromise) => {
return new Promise((resolve, reject) => {
//retrieve an array of every promise status
var promisesStatus = getPromisesStatus();
executeMappingStep(promisesStatus[0], myFirstPromise, null).then(result => {
return convertToXml(result);
}).then(result => {
var parameters = {
value1 : result,
value2: [1,2,3],
value3: "some string",
value4: 10
};
return executeMappingStep(promisesStatus[1], mySecondPromisePromise, parameters);
}).then(result => resolve(result)).catch(err => reject(err));
});
};
var myFirstPromise = function() {
return new Promise(function (resolve, reject) {
//some logic for first promise
});
}
var mySecondPromise = function(firstParam, secondParam, thirdParam, fourthParam) {
return new Promise(function (resolve, reject) {
//some logic for second promise with its params
});
}
Probably, in the function addArrayOfParameters I need to loop into every property on the object (I realized that an array would not work), like this
Is there a way to programmatically add parameters from an array before its resolved?
Edit 2
The reason behind this logic is much complex, but this is pretty much what I need to solve. Basically, in my code, every promise can be executed with errors and there's a logic involved in the executePromises function which returns the status of every promise whether was successfully executed or not. This logic involves more than 10 promises chained, and if some of them fail e.g: promise number 5, then 6,7 etc, will have to be invoked again in some admin backend.
I don't what to replicate the logic for each promise, so that's why I wanted to create the executeMappingStep function to encapsulate that logic. Probably is hard to explain, I tried to simplify the most as I could. I made a few more changes to explain what I just said.