This can be demonstrated with an example that enables or disables a ribbon button on the Account entity if the account has any contacts.
Problematic code example:
We will need a Boolean variable called buttonProcessing with a scope outside the method that will store the actual value to be returned to the Enable Rule. For safety, this variable can have an initial value of false. This is so that a ribbon button is not enabled while the asynchronous call is awaiting its response.
Corrected code example:
This ribbon button is now enabled or not based a value set during an asynchronous callback.
There is a performance consideration that we should take into account when calling refreshRibbon. All buttons in the form ribbon will be refreshed, so this may have an impact if a lot of buttons have been added to form. Fortunately, if the above pattern is followed, the logic behind each custom Enable Rule will be executed only once per form load.
To read more about the refreshRibbon method, please see https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/formcontext-ui/refreshribbon