Promise support in Lightning Components

Promises are supported in Lightning Components; however, care must be taken, in order to not use them when the action is marked as storable; the callback function should be wrapped in $A.getCallback(). (This is because Promises are asynchronous functions. Any asynchronous function, if used in the Lightning component framework, is wrapped in $A.getCallback(), to ensure that the framework re-renders the modified component and processes any queued actions.)

The following snippet shows the generic pattern to follow when using Promises. Note that the following code assumes that the snippet is part of the helper file:

({   getAccounts : function(component,event){ var fetchaccountAction = component.get("c.getAccounts"); ...

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.