我有两个api,我想在两个api都完成时做另一项工作, 而另一个问题是,如果我想充当链,则应先运行然后再运行后者,在控制台中未定义write
var promiseA = service.getAppReciverServiceList(function (data) {
self.chargeMoney = data;
});
var promiseB = service.getAppReciverServiceList(function (data) {
self.chargeMoney = data;
return data;
});
setTimeout(function () { console.log(promiseB); }, 2000);
q.all([
promiseA, promiseB
]).then(function (data) {
console.log(data)
//Array of result [resultOfgetServiceDetails1, resultOfgetServiceDetails2]
scope.variable = data;
});
答案 0 :(得分:0)
我找到了答案,
如果要使用链,则表示一个api响应,然后是其他api工作:
var promise = $q(function (resolve, reject) {
service.getAppReciverServiceList(function (data) {
console.log("1")
resolve(data);
})
});
promise.then(function (data) {
console.log(data, "2")
});
如果我们需要所有的api响应,然后执行其他工作:
var deffered1 = $q.defer();
service.getAppReciverServiceList(function (data) {
deffered1.resolve(data);
})
var deffered2 = $q.defer();
service.getAppReciverServiceList(function (data) {
deffered2.resolve(data);
});
$q.all([deffered1.promise, deffered2.promise]).then
(
function (data) {
console.log(data);
},
function () {
console.log('an error occured');
// error
}
);