当最后一次调用需要其中一个承诺时如何使用 $q.all
How to use $q.all when one of the promises is needed for one last call
我必须进行 3 次调用,完成后将 return 一个对象。前两个调用是独立的,但是第三个调用需要一个数据值作为参数之一,该数据值是前两个调用之一的 returned。下面的模式搞砸了,我试图避免在我调用 myFunction 的地方进行第三次调用,关于如何解决这个问题的任何想法
function myFunction(){
var promise1 = $http({method: 'GET', url: 'a/pi-o-url', cache: 'true'});
var promise2 = $http({method: 'GET', url: '/api-v-url', cache: 'true'});
var myNewObj = {obj1:{}, obj2: {}, obj3: {}};
$q.all([promise1, promise2])
//i'd like to somehow make all the calls in one single function (within myFunction and juts return a single object)
}
我不想做:
myFunction()
.then(function(data){
myNewObj.obj1 = data[0];
myNewObj.obj2 = data[1];
myService(data[0].id).then(function(moreData){
myNewObj.obj3 = moreData;
return moreData;
})
有什么想法吗?
您可以执行以下操作:
function myFunction() {
var promise1 = $http({ method: 'GET', url: 'a/pi-o-url', cache: 'true' });
var promise2 = $http({ method: 'GET', url: '/api-v-url', cache: 'true' });
return $q.all([promise1, promise2]).then(function (data) {
var myNewObj = {
obj1: data[0],
obj2: data[1]
};
return myService(data[0].id).then(function (moreData) {
myNewObj.obj3 = moreData;
return myNewObj;
});
}
}
这样,对于 promise chaining,来自 myFunction
的承诺将通过完整的对象解决。
您可以并行调用前 2 个 promise,然后链接第 3 个 promise
function myFunction(){
var promise1 = $http({method: 'GET', url: 'a/pi-o-url', cache: 'true'});
var promise2 = $http({method: 'GET', url: '/api-v-url', cache: 'true'});
$q.all([promise1, promise2])
.then(function([result1, result2]) {
// after the first 2 promises resolve, create the 3rd promise
var promise3 = $http({method: 'GET', url: '/api-v-url', cache: 'true'});
// this will return an array with 3 promise values
return $q.all[result1, result2, promise3];
})
}
我必须进行 3 次调用,完成后将 return 一个对象。前两个调用是独立的,但是第三个调用需要一个数据值作为参数之一,该数据值是前两个调用之一的 returned。下面的模式搞砸了,我试图避免在我调用 myFunction 的地方进行第三次调用,关于如何解决这个问题的任何想法
function myFunction(){
var promise1 = $http({method: 'GET', url: 'a/pi-o-url', cache: 'true'});
var promise2 = $http({method: 'GET', url: '/api-v-url', cache: 'true'});
var myNewObj = {obj1:{}, obj2: {}, obj3: {}};
$q.all([promise1, promise2])
//i'd like to somehow make all the calls in one single function (within myFunction and juts return a single object)
}
我不想做:
myFunction()
.then(function(data){
myNewObj.obj1 = data[0];
myNewObj.obj2 = data[1];
myService(data[0].id).then(function(moreData){
myNewObj.obj3 = moreData;
return moreData;
})
有什么想法吗?
您可以执行以下操作:
function myFunction() {
var promise1 = $http({ method: 'GET', url: 'a/pi-o-url', cache: 'true' });
var promise2 = $http({ method: 'GET', url: '/api-v-url', cache: 'true' });
return $q.all([promise1, promise2]).then(function (data) {
var myNewObj = {
obj1: data[0],
obj2: data[1]
};
return myService(data[0].id).then(function (moreData) {
myNewObj.obj3 = moreData;
return myNewObj;
});
}
}
这样,对于 promise chaining,来自 myFunction
的承诺将通过完整的对象解决。
您可以并行调用前 2 个 promise,然后链接第 3 个 promise
function myFunction(){
var promise1 = $http({method: 'GET', url: 'a/pi-o-url', cache: 'true'});
var promise2 = $http({method: 'GET', url: '/api-v-url', cache: 'true'});
$q.all([promise1, promise2])
.then(function([result1, result2]) {
// after the first 2 promises resolve, create the 3rd promise
var promise3 = $http({method: 'GET', url: '/api-v-url', cache: 'true'});
// this will return an array with 3 promise values
return $q.all[result1, result2, promise3];
})
}