功能不能同步运行

时间:2019-09-02 10:05:34

标签: javascript angularjs

我有4个功能,calculateSingle, calculateAll, processsave

process调用了一项服务,该服务保存了我重新计算的值,并且应始终在calculateAll完成后进行。就我而言,这不会发生。 save决定以某种方式甚至在所有calculateSingle调用之前运行约4毫秒。

这是我的方法。我将save函数作为对calculateAll函数的回调。请考虑我编写的伪代码。

$scope.calculateSingle = function() {
  //call web service to calculate single row in a table
}

$scope.calculateAll = function(callback) {
  var promises = [];

  for(var i in numberOfRows) {
    promises.push($scope.calculateSingle(i));
  }

  //combine promises to call calculateSingle as many times as there are rows
  $q.all(promises).then(
    function successCallback(response) {
      //take the response and apply all the recalculated values onto view
    }
  ).then(
    //SHOULD BE CALLED AFTER CALCULATE IS FINISHED AND VALUES ARE REASSIGNED
    //This is the $scope.save() function passes as a callback function
    if(callback) {
      callback();
    }
  );
}

$scope.process = function() {
  $scope.calculateAll($scope.save());
}

$scope.save = function() {
  //run the save
}

1 个答案:

答案 0 :(得分:3)

{ "text": "Hello {0} World {1} This is {2} test text" } 呼叫立即保存;如果要将其作为回调传递,则应使用$scope.save()

$scope.save