Using the aura method asynchronously

The aura method discussed in the preceding section has return statements and is synchronous. Most often, when you are working with server-side code to fetch values using @AuraEnabled methods, it's asynchronous. If you use return statements, you will notice that aura methods run before the server returns the value; so, it's important to pass the function and use callback (https://developer.mozilla.org/en-US/docs/Glossary/Callback_function).

Let's demonstrate how callback works, with the help of the following example code.

Let's create a child component (we will assume it's a component API, which can be reused across various components). The controller code to fetch the contacts is as follows:

public with ...

Get Learning Salesforce Lightning Application Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.