Skip to main content
Minor error in forming the question. Edited the questions compatible with the accepted answer for the community to understand.
Source Link
Rahul Allada
  • 507
  • 2
  • 18
  • 36

How do I getreduce the functions called from helper JS class insteadnumber of getting chained from Controller JS?chaining in CONTROLLER js

How do I get the functions called from helper JS class instead of getting chained from Controller JS?

How do I reduce the number of chaining in CONTROLLER js

Became Hot Network Question
Source Link
Rahul Allada
  • 507
  • 2
  • 18
  • 36

Reduce the number of chainings in AURA

I am new to Aura, and I am stuck on how to proceed with the task given. Actually, in the Init method, there are many chaining that should be optimized.

My controller class

doInit: function (component, event, helper) {
helper.getMetadata(component, event, helper)
  .then(function () {
    return helper.getUserInfo(component, event, helper);
  })
  .then(function () {
    return helper.getContactInfo(component, event, helper);
  })
  .then(function () {
    return helper.getIPAddress(component, event, helper);
  })
  .then(.... //Truncated for the brevity
  .catch(function (error) {
    helper.showError(component, event, helper, error);
    let spinner = component.find("spinner");
    $A.util.addClass(spinner, "slds-hide");
  });

My helper class:

    getMetadata: function(component, event, helper) {
    var self = this;
    return new Promise(
      $A.getCallback(function(resolve, reject) {
        var action = component.get("c.getMetadata");
        action.setCallback(this, function(response) {
          var errors = response.getError();
          if (errors) {
            if (errors[0] && errors[0].message) {
              helper.showError(component, event, helper, errors[0].message);
            }
          }
          var state = response.getState();
          if (state === "SUCCESS") {
            var response = response.getReturnValue();
            var wrapper = JSON.parse(response);
            component.set("v.webCaseWrapper", wrapper);
            resolve();
          } else {
            reject($A.get("$Label.c.cs_get_metadata_error"));
          }
        });
        $A.enqueueAction(action);
      })
    );
  },

    getUserInfo: function(component, event, helper) {
    return new Promise(
      $A.getCallback(function(resolve, reject) {
        var runningUserId = component.get("v.runningUserId");
        if (runningUserId && runningUserId != "0051U000000sBUGQA2") {
          var action = component.get("c.getUserInfo");
          action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
              var response = response.getReturnValue();
              component.set("v.name", user.Name);
              component.set("v.email", user.Contact.Email);
              }
              resolve();
            } else {
              reject($A.get("$Label.c.cs_get_user_info_error"));
            }
          });
          $A.enqueueAction(action);
        } else {
          resolve();
        }
      })
    );
  },
 //Similar functions for getIPAddress, getContactInfo

How do I get the functions called from helper JS class instead of getting chained from Controller JS?