Using Promise for Apex Server-Side request in Lightning Component

In Lightning Components, each communication with Apex Server-Side is handled as a Javascript async request, and the callback you set is called after the server-side action is completed. A server-side action can return any JSON object, such as sObject or a map of name-value pairs.

The followoing sample code shows how a server-call works in a Lightning Component.




If there have multiple requests, you need to define multiple actions and callback functions for them, then the source code may looks like below :


Using Promise in Javascript is a way to avoid this “Callback Hell“, and fortunately, Promise can be used in Lightning Compoenent, as below:



Then the source code looks more clean and more elegant, isn’t it?

More about Javascript Promise you can reference here:

Enjoy it!