[SalesForce] Using Callback when calling server side function

I load some values using <aura:handler name="init" value="{!this}" action="{!c.loadOptions}" /> in my component. I want these values to be populated when my component is loaded. Basically it is records I show as a dropdown list. I call the server side controller function to get the records and populate them in an attribute.

My question: I used a callback function when setting up the values of the response received from the server side function. But is there a way to get the results right away meaning without callback. I am thinking if I use callback there could be a delay in showing the result to User. In general I am trying to see if this can be possible without implementing a callback function. Below is my high level component, client side controller and server side controller:


<aura:component controller="AController">
 <aura:handler name="init" value="{!this}" action="{!c.loadOptions}" />
 <aura:attribute name="theValues" type="Account" description="" />
        <lightning:select name="mySelect" label="Accounts" aura:id="mySelect" value="{!v.selectedValue}">
            <option value=""> </option>
            <aura:iteration items="{!v.options}" var="item">
                <option text="{!item.Name}" value="{!item.value}" selected="{!item.selected}"/>


Client side controller:

   loadOptions: function (component, event, helper) {
        // Create the action
        var action = component.get("c.getAccounts");

        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                component.set("v.options", response.getReturnValue());
            else {
                console.log("Failed with state: " + state);
      // Send action off to be executed


Server side controller:

public with sharing class AController {
        public static List<Account> getAccounts() {
               // Check to make sure all fields are accessible to this user
            return [SELECT Id, Name FROM Account];


Generally asking, Can server side controller be called without using callback style and $A.enqueueAction(action);. I am wondering if this could delay the response from the server and user might not be able to see the result on time.

Best Answer

The only way you could not use callbacks is if you were to hardcode the values into the component, but since you are getting a list of accounts, this is not possible.

Every component on Lightning is thought to have great performance for the user, doesn't matter if the server response takes too long to show up. When you access a page like Process Builder, for example, you are getting a page that is filled with callbacks. Basically every action you make in there is firing a callback that calls the server, get info, and then return to the page. While the data is being fetched the component is set to show the loading spinning wheel in your screen, so you could try to imitate that.

Also, as far as I know, there is no synchronous requests made to the server with the Lightning Framework, only asynchronous.

Considering that it is pretty rare for Salesforce's servers to perform poorly, if you are to receive delayed responses, it will be more likely because of your users' internet connection, and that would happen anyway if you were able to use synchronous requests or Visualforce.