+ Start a Discussion

Synchronous back-end actions / async delayed too much?

On a Lightning Component, when calling an action on the Apex back-end using $A.enqueueAction(...), it takes a whileee to complete (up to 30 secs!). The action in the back-end is just a simple DML.

By reading the docs and inspecting the browser network debug logs, we understand this is because the actions are hold in a queue till some sort of poller/spooler thread processes it and dispatches these all together to the backed.

- Is there a way to execute actions inmediately, without enqueuing? Or control the frequency of the dispatching job?
- Or is it that we are something wrong, and $A.enqueueAction(...) is not the correct way to invoke an @AuraEnabled Apex method?

Thanks a lot!
Best Answer chosen by agrassi
Maiquel Cabrera 6Maiquel Cabrera 6
When you execute lightning framework actions outside the component lifecycle you have to wrap your code in a function and call it like this:

  $A.getCallback(function () {//your code});

This problem usually happens when you are binding framework actions to UI events using a library like jQuery:

     $(button).on('click', function () {
        // Force the queue to execute the actions immediately
         $A.getCallback(function () {});

Hope this can help. Thanks !