2

I'm having one method in lightning controller which will call another method in helper class. But it's not executing in the order I coded. I know JavaScript doesn't wait for the response of the previous method to get executed and will move on to the next one.

I know I have to use call back here. But as far as I read "setCallBack" needs to call a server side method to execute.

Please give any idea to proceed on this. Below is my code. I want to disable the lightning button before the alert. But alert comes first and then button is disabled.

Controller:

saving : function(component, event, helper){
    component.set("v.dis",'true');
    helper.callhelper(component, event, helper);
}

Helper:

callhelper : function (component, event, helper){
    alert('After disabling');
}

1 Answer 1

6

In this case, you just need to use setTimeout:

saving : function(component, event, helper){
    component.set("v.dis",'true');
    setTimeout($A.getCallback(helper.callhelper.bind(this, component, event, helper)),0);
}

$A.getCallback makes sure you're in the Aura life cycle, bind returns a function reference with the correct parameters, and setTimeout(..., 0) will execute after the latest Aura rendering cycle.

1
  • Thanks @sfdxfox. It worked in the way I expected.
    – uma451
    Commented Mar 6, 2019 at 11:53

You must log in to answer this question.

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