gpt4 book ai didi

javascript - 取决于两个 promise

转载 作者:行者123 更新时间:2023-12-03 06:36:20 25 4
gpt4 key购买 nike

我需要两个独立的数组来在函数内使用它们。所以我返回 promise 。但是当它到达 arrayOne.length 时,它不会等到 promise 解析。那么如何让它发挥作用呢?

this.workWithArrays = function () {
var arrayOne = this.getArrayOne();
var arrayTwo = this.getArrayTwo();

for (var i = 0; i < arrayOne.length; i++) {
.....
}

getArrayOne 和 getArrayTwo 基本相同:

this.getArrayOne = function() {
return $http.get('/1').then(
function success(response) {
return response.data;
},
function error(data) {
console.log(data);
}
);
}

最佳答案

注入(inject)$q服务并使用它的 all方法:

this.workWithArrays = function () {
var arrayOne = this.getArrayOne();
var arrayTwo = this.getArrayTwo();
$q.all([arrayOne,arrayTwo]).then(function(responses){
// your logic that needs to run after both promises resolve
});

关于javascript - 取决于两个 promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38184832/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com