JavaScript方法链接或角度$q

JavaScript方法链接或角度$q,javascript,angularjs,angular-promise,method-chaining,Javascript,Angularjs,Angular Promise,Method Chaining,我对JavaScript比较陌生,希望您能耐心等待 我试图链接我的方法调用以异步运行,但有点卡住了 我做了很多搜索,尝试了各种方法,但我遗漏了一些东西 其思想是一个接一个地调用一个方法,但只在第一个方法解决后调用 我正在使用AngularJs,我不确定是使用$q和$defer,还是简单的方法链接,还是完全不同的东西 我见过下面的链接方法: callFirst() .then(function(firstResult){ return callSecond(); }) .then(funct

我对JavaScript比较陌生,希望您能耐心等待

我试图链接我的方法调用以异步运行,但有点卡住了

我做了很多搜索,尝试了各种方法,但我遗漏了一些东西

其思想是一个接一个地调用一个方法,但只在第一个方法解决后调用

我正在使用
AngularJs
我不确定是使用
$q
$defer
,还是简单的方法链接,还是完全不同的东西

我见过下面的链接方法:

callFirst()
.then(function(firstResult){
   return callSecond();
})
.then(function(secondResult){
   return callThird();
})
.then(function(thirdResult){
   //Finally do something with promise, or even return this
});
app.service("githubService", function ($http, $q) {

    var deferred = $q.defer();

    this.getAccount = function () {
        return $http.get('https://api.github.com/users/haroldrv')
            .then(function (response) {
                // promise is fulfilled
                deferred.resolve(response.data);
                // promise is returned
                return deferred.promise;
            }, function (response) {
                // the following line rejects the promise 
                deferred.reject(response);
                // promise is returned
                return deferred.promise;
            })
        ;
    };
});
这个使用
$q
的示例:

callFirst()
.then(function(firstResult){
   return callSecond();
})
.then(function(secondResult){
   return callThird();
})
.then(function(thirdResult){
   //Finally do something with promise, or even return this
});
app.service("githubService", function ($http, $q) {

    var deferred = $q.defer();

    this.getAccount = function () {
        return $http.get('https://api.github.com/users/haroldrv')
            .then(function (response) {
                // promise is fulfilled
                deferred.resolve(response.data);
                // promise is returned
                return deferred.promise;
            }, function (response) {
                // the following line rejects the promise 
                deferred.reject(response);
                // promise is returned
                return deferred.promise;
            })
        ;
    };
});
以下是我的主要功能,哪种方法最适合我的目的,我将如何实现最佳解决方案?

注意:在我的
控制器的这个阶段,我的数据已经从我的
API
调用返回,我只是使用数据填充图形和数据网格:

function formatDataAccordingToLocation(dataFromAPI) {

    $scope.dataModel = DataModelService.dataLoaded();

    dataFromAPI.then(function (data) {

        $scope.totalItems = data.total_tweet_count;

        if ($scope.volumeChartChanged) {

            $scope.volumeChartChange = false;

            configureVolumeChart(data);

        }

        else {
            setSummaryPageData(data);

            setTweetListPageData(data);

            configureVolumeChart(data);

            configureMostMentionedGraph(data);

            configureFollowerGrowthGraph(data);

            configureEngagementsGraph(data);

            configureHashtagsGraph(data);

            configureTweetsVsReTweetsGraph(data);

            configureWordCloudGraph(data);
        }
    })
}
我知道我要求很多,非常感谢你的帮助

研究和资源:

callFirst()
.then(function(firstResult){
   return callSecond();
})
.then(function(secondResult){
   return callThird();
})
.then(function(thirdResult){
   //Finally do something with promise, or even return this
});
app.service("githubService", function ($http, $q) {

    var deferred = $q.defer();

    this.getAccount = function () {
        return $http.get('https://api.github.com/users/haroldrv')
            .then(function (response) {
                // promise is fulfilled
                deferred.resolve(response.data);
                // promise is returned
                return deferred.promise;
            }, function (response) {
                // the following line rejects the promise 
                deferred.reject(response);
                // promise is returned
                return deferred.promise;
            })
        ;
    };
});
$q


根据Paulson Peter和suzo的反馈和评论,以下是我问题的答案:


由于我的主函数(
formatDataAccordingToLocation
)在返回的成功的
$http
调用中,我没有必要用承诺来链接这些方法调用,这样做会延迟我的执行。

根据Paulson Peter和suzo的反馈和评论,以下是我的问题的答案:


由于我的主函数(
formatDataAccordingToLocation
)在返回的成功的
$http
调用中,因此我不需要将这些方法调用与承诺链接起来,这样做会延迟我的执行。

您不需要使用$q表示角度的承诺,$http本身返回承诺,因此,您可以链接
$http.then()
,在上一个问题解决后将调用其中的每个问题。感谢您的回复。我是否需要链接我的主函数(数据已经从我的服务返回),或者我只是按照我的代码一个接一个地调用函数?为了链接函数调用,所有这些函数都应该创建为promise($q示例),然后像第一个示例那样调用它。实际上,您为什么需要实现这一点?我认为没有必要创建承诺函数并一个接一个地调用。是的。你的代码很好。您不必做出承诺并链接请求,因为没有i/o调用会延迟执行。是的,因为您正在$http success return中调用所有这些函数。(即在“then”中)这样就没有问题了。在Angular中,您不需要使用$q表示承诺,$http本身返回一个承诺,因此您可以将
$http.then()
链接到上一个问题解决后将调用的位置。感谢您的回复。我是否需要链接我的主函数(数据已经从我的服务返回),或者我只是按照我的代码一个接一个地调用函数?为了链接函数调用,所有这些函数都应该创建为promise($q示例),然后像第一个示例那样调用它。实际上,您为什么需要实现这一点?我认为没有必要创建承诺函数并一个接一个地调用。是的。你的代码很好。您不必做出承诺并链接请求,因为没有i/o调用会延迟执行。是的,因为您正在$http success return中调用所有这些函数。(即在“then”内)因此没有问题。