使用请求承诺的嵌套异步请求
Nested async requests using request-promise
我正在使用 Visual Studio 在线 API 并尝试通过存储库获取分支统计信息。为此,我嵌套了我的异步调用。我正在使用 request-promise 来解决我的 GET 请求。
我遇到的问题是了解如何在所有分支都添加到顶级模型后return模型:
当我 console.log 结果时,我得到 []
显然是因为它没有解决分支请求。
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);
});
我试图在 foreach 之后添加 .then()
,但显然 forEach
不是 return 承诺。
有什么想法吗?我已经编程了 14 个小时,所以对我来说没有任何意义哈哈。
下面应该解决你的问题,而不是做 forEach
循环我已经用你的承诺链中的 .map()
代替了它。我也在你内心的承诺中做到了这一点。此外,我在完成时做出了内部承诺 return,因此外部地图知道每次迭代何时完成。
我留下了 .finally()
,因为这表明无论填充 model
.
的结果如何,我们总是希望响应用户
我还建议将 .catch()
添加到您的外部和内部承诺中,以确保您正确处理任何错误。按照目前的情况,如果发生错误,则不会处理它并且 model
将被 returned 并且您永远不会知道在内部或 .map()
上的迭代之一发生错误外在的承诺。
另外值得注意的是 request-promise
使用 bluebird
实现了 A+ Promises。
var model = [];
rp(options)
.map(function(repository) {
var repo = {
id: repository.id,
name: repository.name,
branches: []
};
var branchOptions = options;
branchOptions.url = config.endPoints.base + config.endPoints.branches(repository.id);
return rp(branchOptions)
.map(function(branch){
repo.branches.push({
name: branch.name,
behind: branch.behindCount,
ahead: branch.aheadCount
});
})
.then(function() {
model.push(repo);
});
})
.finally(fuction() {
console.log(model);
return res.json(model);
});
我正在使用 Visual Studio 在线 API 并尝试通过存储库获取分支统计信息。为此,我嵌套了我的异步调用。我正在使用 request-promise 来解决我的 GET 请求。
我遇到的问题是了解如何在所有分支都添加到顶级模型后return模型:
当我 console.log 结果时,我得到 []
显然是因为它没有解决分支请求。
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);
});
我试图在 foreach 之后添加 .then()
,但显然 forEach
不是 return 承诺。
有什么想法吗?我已经编程了 14 个小时,所以对我来说没有任何意义哈哈。
下面应该解决你的问题,而不是做 forEach
循环我已经用你的承诺链中的 .map()
代替了它。我也在你内心的承诺中做到了这一点。此外,我在完成时做出了内部承诺 return,因此外部地图知道每次迭代何时完成。
我留下了 .finally()
,因为这表明无论填充 model
.
我还建议将 .catch()
添加到您的外部和内部承诺中,以确保您正确处理任何错误。按照目前的情况,如果发生错误,则不会处理它并且 model
将被 returned 并且您永远不会知道在内部或 .map()
上的迭代之一发生错误外在的承诺。
另外值得注意的是 request-promise
使用 bluebird
实现了 A+ Promises。
var model = [];
rp(options)
.map(function(repository) {
var repo = {
id: repository.id,
name: repository.name,
branches: []
};
var branchOptions = options;
branchOptions.url = config.endPoints.base + config.endPoints.branches(repository.id);
return rp(branchOptions)
.map(function(branch){
repo.branches.push({
name: branch.name,
behind: branch.behindCount,
ahead: branch.aheadCount
});
})
.then(function() {
model.push(repo);
});
})
.finally(fuction() {
console.log(model);
return res.json(model);
});