0

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Mauro Bilotti
  • 5,628
  • 4
  • 44
  • 65

2 Answers2

0

Probably, I have complicated things a lot here, but anyway, this is the solution that I wanted. It's good to know that the .then function can invoked in other place and the promises can be created with the needed arguments and then sent as parameter.

With that said:

var executePromises = (myFirstPromise, mySecondPromisePromise) => {
    return new Promise((resolve, reject) => {
        //retrieve an array of every promise status
        var promisesStatus = getPromisesStatus();

        executeMappingStep(promisesStatus[0], myFirstPromise()).then(result => {
                return convertToXml(result);
        }).then(result => {

            var mySecondPromisePromiseParameter = mySecondPromise(result, [1,2,3], "some string", 10);   

            return executeMappingStep(promisesStatus[1], mySecondPromisePromiseParameter);
        }).then(result => resolve(result)).catch(err => reject(err));
    });
};

An then, the executeMappingStep just receives the promise and invokes the .then internally, this way:

var executeMappingStep = (promiseStatus, myPromise) => {
    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);    

        myPromise.then(result => resolve(result)).catch(err => reject(err));
    });
};
Mauro Bilotti
  • 5,628
  • 4
  • 44
  • 65
-1

Probably use async library may resolve your issue. Try to run code on your console and look into that.

With async library:

var async = require("async");
async.waterfall([
    function (callback) {
        var methodParam = [1, 2, 3];
        console.log("first method called");
        callback(null, methodParam);
    },
    function (methodParams, callback) {
        var newParams = [4, 5, 6];
        newParams = methodParams.concat(newParams);
        console.log("second method called", methodParams);
        callback(null, newParams);
    }
], function (err, res) {
    if (err) {
        console.log("error occured");
    } else {
        console.log("final result", res);
    }
});

With promise:

var promise1 = (() => {
    return new Promise((resolve, reject) => {
        var methodParam = [1, 2, 3];
        console.log("first promise called");
        resolve(methodParam);
    });
});

var promise2 = ((methodParams) => {
    return new Promise((resolve, reject) => {
        var newParams = [4, 5, 6];
        newParams = methodParams.concat(newParams);
        console.log("second promise called", methodParams);
        resolve(newParams);
    });
});

promise1()
    .then((resPromise1) => {
        return promise2(resPromise1);
    }).then((resPromise2) => {
        console.log("final result", resPromise2);
    });
halfer
  • 19,824
  • 17
  • 99
  • 186
Dipak
  • 2,248
  • 4
  • 22
  • 55
  • This does not seems to resolve the problem that I have. In every promise I need to use responses from the previous calls as in request. This means that in "then" function of the firstPromise, I need to invoke the second one. PD: I didn't downvote. – Mauro Bilotti Nov 13 '17 at 15:20