Help Interpret Peculiar Way of Handling SUCCESS and ERROR in Lightning Callback

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
6
down vote

favorite
2












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
,"SUCCESS");

action.setCallback(this, function(response)
// Error Code
,"ERROR");

$A.enqueueAction(action);



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.










share|improve this question



























    up vote
    6
    down vote

    favorite
    2












    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
    ,"SUCCESS");

    action.setCallback(this, function(response)
    // Error Code
    ,"ERROR");

    $A.enqueueAction(action);



    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.










    share|improve this question























      up vote
      6
      down vote

      favorite
      2









      up vote
      6
      down vote

      favorite
      2






      2





      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
      ,"SUCCESS");

      action.setCallback(this, function(response)
      // Error Code
      ,"ERROR");

      $A.enqueueAction(action);



      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.










      share|improve this question













      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
      ,"SUCCESS");

      action.setCallback(this, function(response)
      // Error Code
      ,"ERROR");

      $A.enqueueAction(action);



      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.







      lightning-components javascript javascript-controller callback






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 3 hours ago









      Gabriel Rivera

      35113




      35113




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          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.






          share|improve this answer




















            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "459"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f235333%2fhelp-interpret-peculiar-way-of-handling-success-and-error-in-lightning-callback%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            3
            down vote



            accepted










            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.






            share|improve this answer
























              up vote
              3
              down vote



              accepted










              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.






              share|improve this answer






















                up vote
                3
                down vote



                accepted







                up vote
                3
                down vote



                accepted






                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.






                share|improve this answer












                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.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 3 hours ago









                sfdcfox

                231k10178394




                231k10178394



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f235333%2fhelp-interpret-peculiar-way-of-handling-success-and-error-in-lightning-callback%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Comments

                    Popular posts from this blog

                    What does second last employer means? [closed]

                    Installing NextGIS Connect into QGIS 3?

                    One-line joke