angular 中的 $q.all() 解析不佳
$q.all() in angular does not resolve well
我在一家工厂进行 3 个 $http 调用,我遇到了麻烦。
我宣布 4 项承诺:
var promise = $q.defer(),
PBdeferred = $q.defer(),
Rdeferred = $q.defer(),
Pdeferred = $q.defer();
之后我第一次调用 API
$http.get('/pendingBills').then(function(response) {
var PendingBills = ['id', 'path', 'reservas', 'importe', 'fecha'];
PBdeferred.resolve(PendingBills);
});
现在用一个空数组解决最后 2 个承诺(我还没有端点)
Rdeferred.resolve([]);
Pdeferred.resolve([]);
那里是我用$q.all
的地方
$q.all([PBdeferred, Rdeferred, Pdeferred]).then(function (results){
console.log('Results', results);
promise.resolve({
PendingBills: results[0],
Remittances: results[1],
Payed: results[2]
});
});
和return高级别承诺
return promise.promise;
控制台日志显示了承诺,但我认为承诺已解决。
解决这个问题的想法?
您没有正确使用 $q.all
。它需要一个承诺数组(或对象),而不是一个延迟数组。
改变
$q.all([PBdeferred, Rdeferred, Pdeferred])
至
$q.all([PBdeferred.promise, Rdeferred.promise, Pdeferred.promise])
您没有正确使用 promises,因为您使用了 deferred,这是一种破坏 promises 链的反模式。不要使用延迟,只需为每个操作获取承诺,然后使用 $q:
组合它们
var PBpromise = $http.get('/pendingBills').then(function(response) {
return ['id', 'path', 'reservas', 'importe', 'fecha']; // this will return a promise with the array as the resolve value
});
var Rpromise = $q.resolve(); // a promise that is resolved immediately. Later you can replace it with the $http call
var Ppromise = $q.resolve(); // a promise that is resolved immediately. Later you can replace it with the $http call
var promise = $q.all([PBdpromise, Rpromise, Ppromise]).then(function (results){ // $q.all also returns a promise
console.log('Results', results);
return { // this will be the resolve value of the returned $q promise
PendingBills: results[0],
Remittances: results[1],
Payed: results[2]
};
});
请注意,$q.resolve()
仅在 angular 1.4 之后可用。如果您使用以前的版本,请使用 $q.when({})
而不是 $q.resolve()
。
我在一家工厂进行 3 个 $http 调用,我遇到了麻烦。
我宣布 4 项承诺:
var promise = $q.defer(),
PBdeferred = $q.defer(),
Rdeferred = $q.defer(),
Pdeferred = $q.defer();
之后我第一次调用 API
$http.get('/pendingBills').then(function(response) {
var PendingBills = ['id', 'path', 'reservas', 'importe', 'fecha'];
PBdeferred.resolve(PendingBills);
});
现在用一个空数组解决最后 2 个承诺(我还没有端点)
Rdeferred.resolve([]);
Pdeferred.resolve([]);
那里是我用$q.all
的地方$q.all([PBdeferred, Rdeferred, Pdeferred]).then(function (results){
console.log('Results', results);
promise.resolve({
PendingBills: results[0],
Remittances: results[1],
Payed: results[2]
});
});
和return高级别承诺
return promise.promise;
控制台日志显示了承诺,但我认为承诺已解决。
解决这个问题的想法?
您没有正确使用 $q.all
。它需要一个承诺数组(或对象),而不是一个延迟数组。
改变
$q.all([PBdeferred, Rdeferred, Pdeferred])
至
$q.all([PBdeferred.promise, Rdeferred.promise, Pdeferred.promise])
您没有正确使用 promises,因为您使用了 deferred,这是一种破坏 promises 链的反模式。不要使用延迟,只需为每个操作获取承诺,然后使用 $q:
组合它们var PBpromise = $http.get('/pendingBills').then(function(response) {
return ['id', 'path', 'reservas', 'importe', 'fecha']; // this will return a promise with the array as the resolve value
});
var Rpromise = $q.resolve(); // a promise that is resolved immediately. Later you can replace it with the $http call
var Ppromise = $q.resolve(); // a promise that is resolved immediately. Later you can replace it with the $http call
var promise = $q.all([PBdpromise, Rpromise, Ppromise]).then(function (results){ // $q.all also returns a promise
console.log('Results', results);
return { // this will be the resolve value of the returned $q promise
PendingBills: results[0],
Remittances: results[1],
Payed: results[2]
};
});
请注意,$q.resolve()
仅在 angular 1.4 之后可用。如果您使用以前的版本,请使用 $q.when({})
而不是 $q.resolve()
。