2

I have a requirement , where I want to call server methods multiple times passing some value as parameter. I have used javascript promise to call the methods one after another using .then When I am running my piece of code two issues I am facing.

  1. In each then I have a server call . So I have used $A.getCallback but the resolve result is coming as undefined and actual server logic from the first promise executes in some fraction of delay thus result is coming as undefined.

  2. In all the then I have one DML statement , So ideally for each promise there has to be separate transaction but in salesforce debug log all DML from all promises comes under one transactional limit.

PFB snippet:

Promise.resolve()
    .then($A.getCallback(
        function () {
            //client side data manipulation

            return (0);
        }))
    .then($A.getCallback(function recursiveFunction(
        recIndex) {
        //recursive promise logic

        if (somecondition) {

            //call server
            //helper.submethodcalled
            //if success with the sub method then only go for recursion or else stop
            var promise = Promise.resolve()
                .then(
                    function () {
                        console.log('inside rec loop**');
                        return (recursiveFunction(some increment /
                            decrement condition)); // RECURSE!
                    }
                );
            return (promise);

        } else {
            return (0);
        }
    }))
    .then($A.getCallback(function () {
        //final block
    }))
    .catch(function (error) {
        console.log('Failure : ' + error.message);
    });
6
  • For server side calls, you need to pass a callback to the function that calls the server side action. That code is missing in your example, so it's not clear what the server side action can call when the operation completes. If you omitted it, then nothing will be called and the action will be async as you describe. Commented May 25, 2018 at 23:42
  • And why don't you send all the data in a single request to the server and do all the processing in a shot? And as you are performing multiple DML operations and if you think it might be time-consuming process then mark the action as action.setBackground(). Commented May 26, 2018 at 3:25
  • Hi @RobertSussland I have called my callback from helper.submethodcalled . Here I have only provided the flow of my code , not the actual logic. Commented May 26, 2018 at 14:24
  • @itzmukeshy7 No I have such requirement where I want to perform DML on large data set thus I wanted break that in no of chunks and it will hit the method asynchronously but one after another using Promise.then . Commented May 26, 2018 at 14:24
  • Then why you want to use a Promise any way the same you can achieve using the callback action triggers on the response from the server? Commented May 26, 2018 at 14:30

1 Answer 1

0

Thanks for the answer , but I was able to solve both the issues.Instead of recursive promise I am now using java script array.reduce method I was facing by following approach. Please find my below answer in pseudo code.

function: promiseChainingMethod(component, groups) {
    var promise = groups.reduce(

        function reducer(promiseInst, value) {
            var nextThenChain = promiseInst.then(
                function(resultValue) {
                    console.log('do some checking with value return from last promise****' + resultValue);
                    //call your helper logic which has return type of promise with $A.getCallback
                    var apexMethodResult = this.subMethod(component, value);
                    return (apexMethodResult);
                }
            );
            return (nextThenChain);
        },
        Promise.resolve(0) //This will start the promise with 0 value   

    ).then($A.getCallback(function(ress) {
        console.log('final block***' + ress); //post array execution logic , if needed
        console.groupEnd();
    })).catch(function(error) {
        console.log('Failure : ' + error.message); //error handling
    });
},

//helper method to call server and get DML result status
function: subMethod(component, listData) {
    retutn new Promise($A.getCallback(function(resolve, reject) {
        //server call
        var result = //server return data
            if (success)
                resolve(result);
    }));
}

Now my promise is working as expected and for each server call separate transaction is being generated , which was my 2nd issue.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .