Lightning Async Action calls versus JS Promises

The Lightning Developer Guide states:

All the action calls are asynchronous and run in
batches.

If this is the case, does the framework have any concept resembling JavaScript promises?

It seems that the aura:doneWaiting event might fulfill that role, but I’m not clear on if there is a need to pass some specific event to the action, or if it will just handle server responses in the component’s scope.

I’m kicking of an action call (which retrieves a list of Contacts) on component init, then am attempting to pass the result to the component in a custom render function. Because of the async nature of the action, my component is rendering before the call successfully returns. Is aura:doneWaiting the way to go here?

Updated:
I may be approaching this the wrong way and can potentially leverage how the framework handles data binding, and rerendering when underlying data changes. Will post a definitive answer if I come across one. I’d still be interested to know if comparing aura:doneWaiting to JavaScript promises is appropriate or not.

Answer

Please refer to this article

When we make an Apex call using $A.enqueueAction(), the framework
fires:

waiting event to allow us to show visual indication like “waiting..”
or a “spinner” to the user

doneWaiting event is triggered to allow us to hide any visual
indication

valueChange is fired ONLY IF the callback updates some value

rerender if a value is changed

doneRendering is eventually called

enter image description here

Things To Note:

  1. waiting and doneWaiting are always fired whenever we use $A.enqueueAction()
  2. However valueChange, rerender and doneRendering are only
    fired if our business logic changes some attribute value of the
    component.

PS: You can use rerender() in your custom renderer to write some logic.

Attribution
Source : Link , Question Author : chrisjordanme , Answer Author : Saurabh Mishra

Leave a Comment