0

Take a look at the codes below:

fetchServerData: function (component, event, helper) {
    //calling Apex methods, fetching data etc..

}

updateData: function (component, event, helper) {
    var fetchServerData= component.get("c.fetchServerData");
    $A.enqueueAction(fetchServerData);
    //update logics in JS etc.
}

As you can see, I'm calling the fetchServerData from the updateData method. But, since JS in Salesforce works asynchronously, I want to control the flow of it. I want to execute the fetchServerData first then go to the update logic section, not before that. How can I achieve it? FYI I can only update the updateData method, not the fetchServerData.

1 Answer 1

2

You can use "Promises" to control the flow. Following are few links where you can find some examples.

The goal is to do something like this :

 var action = cmp.get("c.fetchServerData");
    new Promise(function(resolve, reject) {
        action.setCallback(this, function(response) {
            //check the state and do stuff
        });
        $A.enqueueAction(action);
    }).then(
       //your update logic which will execute after the promise is resolved
    );

Following are few links for references and deep dive

https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/js_promises.htm https://www.sitepoint.com/flow-control-callbacks-promises-async-await/

You must log in to answer this question.

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