JavaScript 方法链接或 Angular $q
JavaScript Method Chaining or Angular $q
我对 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
});
而这个使用$q
的例子:
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;
})
;
};
});
以下是我的主要功能,哪种方法最适合我的目的,我将如何实施最佳解决方案?
注意: 在我的 controller
这个阶段,我的数据已经从我的 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);
}
})
}
我知道我的要求很多,非常感谢您的帮助。
研究与资源:
https://docs.angularjs.org/api/ng/service/$q
Chain promises with AngularJS
https://schier.co/blog/2013/11/14/method-chaining-in-javascript.html
根据 Paulson Peter 和 suzo 的精彩反馈和评论,以下是我的问题的答案:
因为我的主要功能 (formatDataAccordingToLocation
) 在我返回的成功 $http
调用中,所以我不需要用承诺链接这些方法调用,并且在做所以,推迟我的处决。
我对 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
});
而这个使用$q
的例子:
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;
})
;
};
});
以下是我的主要功能,哪种方法最适合我的目的,我将如何实施最佳解决方案?
注意: 在我的 controller
这个阶段,我的数据已经从我的 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);
}
})
}
我知道我的要求很多,非常感谢您的帮助。
研究与资源:
https://docs.angularjs.org/api/ng/service/$q
Chain promises with AngularJS
https://schier.co/blog/2013/11/14/method-chaining-in-javascript.html
根据 Paulson Peter 和 suzo 的精彩反馈和评论,以下是我的问题的答案:
因为我的主要功能 (formatDataAccordingToLocation
) 在我返回的成功 $http
调用中,所以我不需要用承诺链接这些方法调用,并且在做所以,推迟我的处决。