Warning: Illegal string offset 'ssb_og_tags' in /var/www/www.xgeek.net/html/wp-content/plugins/simple-social-buttons/simple-social-buttons.php on line 1466

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.

SampleAura.cmp

SampleAuraController.js

SampleAuraController.cls

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

SampleAuraController.js

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

SampleAuraController.js

SampleAuraHelper.js

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

More about Javascript Promise you can reference here:
https://developers.google.com/web/fundamentals/primers/promises

Enjoy it!