Declare a function inside another function

2019-09-04 17:25发布

问题:

i have this function:

 charCtrl.loadDataFromToMonth= function (from,to,year) {
var url = servername+'admin/dashboard/getIncidentDepartByMonthFromTo/'+from+'/'+to+'/'+year;
//alert(url);
function onSuccess(response) {
  console.log("+++++getIncidentDepartByMonthFromTo SUCCESS++++++");

  if (response.data.success != false) {

    $scope.payloadgetIncidentDepartByMonthFromTo = response.data.data;

    var getIncidentDepartByMonthFromTo= $scope.payloadgetIncidentDepartByMonthFromTo;
    // alert('dddd'+JSON.stringify(loadedDataByMission));
    $scope.labelsf = [];
    $scope.qsd = [];
    $scope.dataf = [];
    getIncidentDepartByMonthFromTo.forEach(function(data) {
      var monthNumber=$filter('date')(data.la_date, "MM");
      $scope.labelsf.push($filter('monthName')(monthNumber));
      $scope.dataf.push(data.number);
      $scope.qsd.push('ff','ff','ff');
    //alert($scope.labelsf );
    });
    //alert( $scope.dataf );
   charCtrl.loadDataFromToMonthArrivee(from,to,year);
  } else {
    alert("failure");
  }
  //  $scope.stopSpin('spinner-0');
};

function onError(response) {
  console.log("-------getIncidentDepartByMonthFromTo FAILED-------");
  //$scope.stopSpin('spinner-0');
  console.log(response.data);
  console.log("Inside getIncidentDepartByMonthFromTo error condition...");
};

//----MAKE AJAX REQUEST CALL to GET DATA----
 ajaxServicess.getData(url,username,password, 'GET', '').then(onSuccess, onError);

 };

this function return for exemple: $scope.dataf = ['45','48','255'];

I have a second function, by the way its the same function but it gets data from other Rest service:

charCtrl.loadDataFromToMonthArrivee= function (from,to,year) {
var url = servername+'admin/dashboard/getIncidentArriveeByMonthFromTo/'+from+'/'+to+'/'+year;
//alert(url);
function onSuccess(response) {
  console.log("+++++getIncidentDepartByMonthFromTo SUCCESS++++++");

  if (response.data.success != false) {

    $scope.payloadgetIncidentArriveeByMonthFromTo = response.data.data;

    var getIncidentArriveeByMonthFromTo= $scope.payloadgetIncidentArriveeByMonthFromTo;
    // alert('dddd'+JSON.stringify(loadedDataByMission));
    $scope.labelsf = [];
    $scope.qsd = [];
    $scope.dataf = [];
    getIncidentArriveeByMonthFromTo.forEach(function(data) {
      var monthNumber=$filter('date')(data.la_date, "MM");
      $scope.labelsf.push($filter('monthName')(monthNumber));
      $scope.dataf.push(data.number);
      //$scope.qsd.push('ff','ff','ff');
      //alert($scope.labelsf );
    });
    alert('aqsz'+$scope.dataf );
  } else {
    alert("failure");
  }
  //  $scope.stopSpin('spinner-0');
};

function onError(response) {
  console.log("-------getIncidentDepartByMonthFromTo FAILED-------");
  //$scope.stopSpin('spinner-0');
  console.log(response.data);
  console.log("Inside getIncidentDepartByMonthFromTo error condition...");
};

//----MAKE AJAX REQUEST CALL to GET DATA----
ajaxServicess.getData(url,username,password, 'GET', '').then(onSuccess, onError);

  };

this function return for exemple: $scope.dataf = ['69','50','96'];

my question is: is there a way to declare the second function in the first fuction and get a result like this: this function return for exemple: $scope.dataf = [['45','48','255'],['69','50','96']];

回答1:

I would use promises to wait for the second call to respond and update a variable in the function scope (sibling to the function). Something like this:

var aggregate = [];

charCtrl.loadDataFromToMonth()
.then(function(resp){
  return new Promise(resolve, reject){

    aggregate.push(resp.data); // push the response from the first call

    charCtrl.loadDataFromToMonthArrivee()
    .then(function(resp) {
      aggregate.push(resp.data); // push the result from the second call
      resolve(resp)
    })
  }
})
.then(function(resp){

  console.log(aggregate); // should have both results

})

Or better yet, you can use Promise.all (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all)