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.
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.
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);
});
action.setBackground()
.Promise
any way the same you can achieve using the callback action triggers on the response from the server?