function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Arut JothiArut Jothi 

$A.getCallback()

1) What are some of scenarios where $A.getCallback can be used other than using setTimeout function??
2) Does it make sense to place $A.getCallback inside a Promise constructor while calling a server side controller action? For example, see below
     return new Promise($A.getCallback(function(resolve, reject){
         // Implement the code logic here to call a server side controller
     }));
Raj VakatiRaj Vakati
Yes .. You can able to use return  $A.getCallback inside promise . sample code is here
 
return new Promise(function(resolve, reject) { 
            action.setCallback(this, 
                               function(response) {
                                   var state = response.getState();
                                   if (state === "SUCCESS") {
                                       resolve(response.getReturnValue());
                                   } else {
                                       reject(new Error(response.getError()));
                                   }
                               }); 
            $A.enqueueAction(action);
        });
    },


Refer this link

https://rajvakati.com/2018/05/29/using-promise-in-lightning-component/

 
Arut JothiArut Jothi
@Raj V,

Thanks for the response but you are taling about using Promises in a Lightning component. My question is rather different and very specific to using $A.getCallback while implementing promises in a Lightning component.
Shruti SShruti S
Let's take jQuery for example. jQuery modifies the DOM of a Lightning Component outside the Lightning Context or Rendering Life-cycle. The modifications made by jQuery to the DOM elements runs within the jQuery Script File. Thus the $A.getCallback is handy when you want the context to the Component back even in the execution that's happening outside the Lightning Context.

This is one major use of $A.getCallback(say Aura.getCallback) that I have found.