[SalesForce] show error message after delay in lightning

Here is the situation. I have a @Future method which has callouts is running along side of actual main CLASS. in the callout response, I am receiving some error/success XML, I am saving them in a STRING message. However, I can not show error from the String RETURN as @future will not send anything like that.
So, I am saving that String in another custom Field and updating at the end of FUTURE method.
Now, I want to show that field message in Lightning. Can anyone tell me how can I get it done?
Once we are proceeding, Main class executing and exiting then future method getting executed within a few millisec. I want to wait for 5 seconds after we run it in lightning so that I can use the updated field in @future method to show error.

It's a bit complicated it seems but I am not getting any clue. Please help.
Thank you,
Ashok Kumar.

Best Answer

To answer your actual question, here is how you can call a method after a wait period:

ControllerJS

callbackOnceAfterDelay : function(component, event, helper) {
        var delayInMilliseconds = 5000; //5 seconds
        window.setTimeout(
            $A.getCallback(function() {
                console.log('myHelperMethod EXECUTING NOW... ');
                helper.myHelperMethod(component, event, helper);
            }), delayInMilliseconds
        );     
    }

But, I don't think it is an elegant approach, just because you are assuming that the future method will complete in 5 seconds. Why keep the user waiting if it returns in 1 second? And what if it takes more than 5 seconds... you won't show a message at all!

Because you mentioned that a field will be updated with the error message after the callout returns, why not regularly check for an update to that field like so:

ControllerJS

callbackAfterEveryNSeconds : function(component, event, helper) {
        var intervalInMilliseconds = 1500; //1.5 seconds interval between each call
        var processId = window.setInterval(
            $A.getCallback(function() {
                console.log('myHelperMethod EXECUTING NOW... ');
                helper.setErrorMessageIfUpdated(component, event, helper); 
            }), intervalInMilliseconds
        );
        component.set("v.processId", processId); 
    },

HelperJS

setErrorMessageIfUpdated: function(component, event, helper) {
        //do a server side action here, check if error field has been updated
        //if so, do component.set("v.errorMessage", response.getReturnValue()); 
        //show the error message if not blank
        //clear the interval for repeating anymore like so: window.clearInterval(component.get("v.processId"));
    },

Notice that window.setTimeout executes once after a set delay. On the other hand, window.setInterval executes over and over after a set time period (so make sure it stops after your callout returns).

And also, did you know that you can detect changes to a specific value and fire a method when that happens? Like so: (just food for thought... you might end up not needing it at all)

<aura:handler name="change" value="{!v.record.My_Callout_Error_Message_Field__c}" action="{!c.showErrorMessageIfAny}"/>
Related Topic