I didn't write but rather I'm reviewing the following Lightning JS function:

InitializeComponent : function (component) {

        var recId = component.get("v.recordId");

        var action = component.get("c.Init");
        action.setParams({ "recordId" : recId });
        action.setCallback(this, function(response) {
            var resp = response.getReturnValue();
            if (component.isValid()) {    
                // Success Code             }

        action.setCallback(this, function(response) {
            // Error Code


I've never seen and am trying to understand the way successes and failures of server side actions are being handled in this code.

It's less intuitive to me (but maybe more efficient?) than the developer guide's way here:

action.setCallback(this, function(response) {
    var state = response.getState();
    if (state === "SUCCESS") {
        // do something with return value
    } else if (state === "INCOMPLETE") {
        // do something
    } else if (state === "ERROR") {
        // do something with error

I don't understand how setting up two call backs, making the SUCCESS action state an additional parameter in one and ERROR in the other, handles what to do in each of those conditons.

1 Answer 1


This is in the documentation, under JavaScript API / Action.

setCallback (Object scope, function callback, String name)

Sets the callback function that is executed after the server-side action returns. Call a server-side action from a client-side controller using callback. Note that you can register a callback for an explicit state, or you can use 'ALL' which registers callbacks for "SUCCESS", "ERROR", and "INCOMPLETE" (but not "ABORTED" for historical compatibility). It is recommended that you use an explicit name, and not the default 'undefined' to signify 'ALL'. The valid names are: * SUCCESS: if the action successfully completes. * ERROR: if the action has an error (including javascript errors for client side actions) * INCOMPLETE: if a server side action failed to complete because there is no connection * ABORTED: if the action is aborted via abort() * REFRESH: for server side storable actions, this will be called instead of the SUCCESS action when the storage is refreshed.

Basically, setCallback does allow you to register up to five different handlers, two of which aren't covered by the default undefined/"ALL" scenario. This can make code easier to read, especially if you have a common error handler, for example. This isn't well explained in any of the trailheads, but is officially supported.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .