我正在使用Visual Studio Online API并试图通过存储库获取分支统计信息。要做到这一点,我嵌套了我的异步调用。我正在使用request-promise来解决我的GET请求。使用request-promise嵌套的异步请求
我有是理解如何返回模式后,所有的树枝都被添加到顶级型号的问题:
当我CONSOLE.LOG结果我得到[]
明显,因为它不是” t解决分支请求。
var model = [];
rp(options)
.then(function(repos) {
repos.value.forEach(function(repository) {
var repo = {
id: repository.id,
name: repository.name,
branches: []
};
var branchOptions = options;
branchOptions.url = config.endPoints.base + config.endPoints.branches(repository.id);
rp(branchOptions)
.then(function(branches) {
branches.value.forEach(function(branch) {
repo.branches.push({
name: branch.name,
behind: branch.behindCount,
ahead: branch.aheadCount
});
})
}).then(function() {
model.push(repo);
});
});
}).finally(function(){
console.log(model);
res.json(model);
});
我尝试添加.then()
在foreach之后,但显然forEach
不返回的承诺。
任何想法?我已经编程了14个小时,所以对我来说没有任何意义。
一个对每个没有,但如果你使用。降低,而不是用一个承诺链中,减少将返回一个。 :) –
使用'map'而不是'forEach',这样你就可以得到一组promise;然后将其送入'Promise.all'。或者只是使用'Promise.map',因为你正在使用蓝鸟。 – Bergi