我用服务创建了2个服务调用跟随控制器.第二个响应在此之前出现.我想做的就像我需要第一个响应第一个和第二个响应秒.但我只是坚持异步和同步请帮我解决.
第二次通话取决于第一次通话.例如,如果第一次调用返回10记录,那么我必须从第一次响应中调用第二次Web服务10次.所以我使用循环,但它是不正确的.
var mycompaigndata = [];
asyncService.loadDataFromUrls($http.get(WSURL + 'api/first/',
{
headers:
{
"Authorization":'Bearer <my-token>'
}
}))
.then(function(data)
{
console.log(data);
});
asyncService.loadDataFromUrls($http.get(WSURL + 'api/second',
{
headers:
{
"Authorization":'Bearer <my-token>'
}
}))
.then(function(data)
{
console.log(data);
});
Run Code Online (Sandbox Code Playgroud)
app.service('asyncService', function($http, $q)
{
return {
loadDataFromUrls: function(url)
{
var deferred = $q.defer();
var urlCalls = [];
urlCalls.push(url);
$q.all(urlCalls)
.then(
function(results)
{
deferred.resolve(results)
},
function(errors)
{
deferred.reject(errors);
},
function(updates)
{
deferred.update(updates);
});
return deferred.promise;
}
};
});
Run Code Online (Sandbox Code Playgroud)
要确保在第一个呼叫完成后执行第二个呼叫,请将第二个呼叫置于then
第一个呼叫之内.要根据第一个呼叫的结果数量进行多次"第二次"呼叫,请使用$q.all
.
asyncService.loadDataFromUrls('api/first/')
.then(function(firstData) {
//assuming firstData is an array of 'x' items, do a call for each of these items:
console.log('results of first call holds ' + firstData.length + ' items');
var promises = [];
for(var i = 0; i<firstData.length; i++){
var id = firstData[i].id;//you can use this to pass to the second call
promises.push(asyncService.loadDataFromUrls('api/second'));
}
return $q.all(promises);
})
.then(function(results) {
//'results' is an array of results, the nth item holds the result of the 'nth' call to loadDataFromUrls
for(var i = 0; i<results.length; i++){
console.log('result nr. ' + i + ' :' + results[i])
}
});
Run Code Online (Sandbox Code Playgroud)
通过使用return $q.all(promises)
,你可以避免厄运的承诺金字塔,并保持扁平结构.
您的服务代码不再需要循环.作为旁注,您可以缩短服务代码并避免使用"显式承诺构造反模式"(请参阅此处),如下所示:
app.service('asyncService', function($http, $q)
{
return {
loadDataFromUrls: function(url)
{
return $http.get(WSURL + url, {
headers: {
"Authorization": 'Bearer <my-token>'
}
}).then(function(response){ return response.data; });
}
};
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
37986 次 |
最近记录: |