2017-05-25 42 views
1

我知道以前也有类似的问题,但没有一个例子对我有意义。 (任何我发现没有解释我需要的清晰度的基础知识。)如何链接多个单独的JavaScript函数

我有四个AngularJS函数。每个调用REST服务并执行与其他任何函数无关的东西。例如。

$scope.step1 = function() { $http({ 
    method: 'GET', 
    url: "http://www/end/point", 
    cache: true, 
    headers: { "Accept": "application/jsonp;odata=verbose" } 
}).success(function (data, status, headers, config) { 
    $scope.step1data = data; 
}).error(function (data, status, headers, config) { 
    $scope.logError(data); 
});}; 

我想打电话给四大功能于序列

  1. $ scope.step1
  2. $ scope.step2
  3. $ scope.step3
  4. $ scope.step4

并捕获遇到的任何错误。

我缩小了代码到下面,但它不适合我。任何帮助将不胜感激。

$scope.step1().then(function() { 
    $scope.step2(); 
}).then(function() { 
    $scope.step3(); 
}).then(function() { 
    $scope.step4(); 
}).catch(function(e) { 
    console.log(e); 
}); 

回答

1

首先,改变你的第一步,第二步,第三步和第四步,以返回象下面这样的承诺:

$scope.step1 = function() { 
    return $http({ 
    method: 'GET', 
    url: "http://www/end/point", 
    cache: true, 
    headers: { "Accept": "application/jsonp;odata=verbose" } 
    })}; 

    $scope.step2 = function() { 
    return $http({ 
    method: 'GET', 
    url: "http://www/end/point", 
    cache: true, 
    headers: { "Accept": "application/jsonp;odata=verbose" } 
    })}; 

    $scope.step3 = function() { 
    return $http({ 
    method: 'GET', 
    url: "http://www/end/point", 
    cache: true, 
    headers: { "Accept": "application/jsonp;odata=verbose" } 
    })}; 

    $scope.step4 = function() { 
    return $http({ 
    method: 'GET', 
    url: "http://www/end/point", 
    cache: true, 
    headers: { "Accept": "application/jsonp;odata=verbose" } 
    })}; 

然后,把它们连在一起就像这样:

$scope.step1().then(function (step1data) { 
    $scope.step1data = step1data; 
    $scope.step2().then(function (step2Data) { 
    $scope.step2Data = step2Data; 
     $scope.step3().then(function (step3Data) { 
     $scope.step3Data = step3Data; 
     $scope.step4().then(function (step4Data) { 
      $scope.step4Data = step4Data; 
     }); 
     }); 
    }); 
}) 
2

你需要从每一步功能的承诺回到你then()回调,使得其产生的诺言等待这个承诺:

$scope.step1().then(function() { 
    return $scope.step2(); 
})... 
0

你需要使用promise,在你的控制器中,你应该注入$ q来处理promise。

angularApp.controller('YourController', ['$q', '$scope', 
    function ($q, $scope) { 

     $scope.firstFunction = function() { 
      var deferred = $q.defer(); 
      //Do things you want here... 

      console.log(1); 

      deferred.resolve(); 
      return deferred.promise; 
     }; 

     $scope.secondFunction = function() { 
      var deferred = $q.defer(); 
      //Do things you want here... 

      console.log(2); 

      deferred.resolve(); 
      return deferred.promise; 
     }; 

     $scope.firstFunction().then(function() { 
      $scope.secondFunction.then(function() { 
       console.log(3) 
      }) 
     }) 
    }]); 

控制台功能日志上面会打印出:

1 
2 
3 
0

你可以用任何一种逻辑的JavaScript提供,如果您同步执行你的代码通过nsynjs混合异步函数。这里是你的代码可能需要如何转变:

第1步:总结异步功能分为通用nsynjs感知包装:

// generic function to retrieve url 
// ctx is a reference to caller pseudo-thread context 
var httpUrl = function(ctx,url,$http) { 
    var res={}; 
    $http({ 
     method: 'GET', 
     url: url, 
     cache: true, 
     headers: { "Accept": "application/jsonp;odata=verbose" } 
    }) 
    .success(function (data, status, headers, config) { 
     res.data = data; 
     ctx.resume() // tells nsynjs to resume caller function 
    }).error(function (data, status, headers, config) { 
     ctx.resume(data) // resume caller function with exception 
     // or replace with these 2 lines if you don't want exception 
     // res.error = data; 
     // ctx.resume() 
    }); 
    return res; 
}; 
getUrl.nsynjsHasCallback = true; // this tells nsynjs engine that it 
           // should pause execution and wait until ctx.resume() is called from callback 

第2步:写你的逻辑,如果它是同步的,并把它进入功能:

function synchronousCode(param1, param2) { 
    var step1 = function() { 
     var data = httpUrl(nsynjsCtx,"nsynjs/examples/data/file1.json").data; 
     console.log("data is ready at this point:",data); 
     // do staff this data 
     return data; 
    }; 
    var step2 = function() { 
     var data = httpUrl(nsynjsCtx,"nsynjs/examples/data/file2.json").data; 
     console.log("data is ready at this point:",data); 
     // do staff this data 
     return data; 
    }; 

    console.log(step1(param1) + step2(param2)); // do more staff with data 
} 

第3步:通过nsynjs运行您的同步代码:

nsynjs.run(synchronousCode,{},"value for param1","value for param1",function(){ 
    console.log("Synchronous Code done"); 
}) 

更多示例:https://github.com/amaksr/nsynjs/tree/master/examples

相关问题