我有一个AngularJS函数,在其中我要进行2个异步调用。这两个调用不相关,但是仅当两个调用都完成并且结果存储在return var中时,函数才应返回。
我尝试了不同的解决方案,并最终使用了以下所示的解决方案。但是它等待第一个完成的过程非常缓慢
function myAngularfunction(a, b) {
var defer = $q.defer();
var test= {
a: "",
b: ""
};
msGraph.getClient()
.then((client) => {
// First Async call
client
.api("https://graph.microsoft.com/v1.0/")
.get()
.then((groupResponse) => {
var result = groupResponse;
test.a= result.displayName;
msGraph.getClient()
.then((client) => {
// Second Async call
client
.api("https://graph.microsoft.com/v1.0/teams/")
.get()
.then((groupResponse) => {
var result = groupResponse;
test.b= result.displayName;
defer.resolve(test);
});
});
});
});
return defer.promise;
}
调用函数
myAngularfunction(a, b).then(function(obj)
我如何在不嵌套的情况下等待同一个函数中的两个调用?或呼叫下一个而不等待第一个完成。
[也许您可以在这样的情况下尝试$:$。when(functiton1,function2).done(()=> {})。但您需要在function1和function 2中添加deffered。
$q.all([promise1, promise2, promise3]).then()
语法。 msGraph.getClient()
,我认为可以避免。