使用BlueBird/Request-Promise链接请求 [英] Chaining Requests using BlueBird/ Request-Promise
问题描述
我正在使用request-promise模块,却没有提到如何链接请求.我目前正在遵循以下语法:
I'm using the request-promise module and have found no mention of how to chain requests. I'm currently following their syntax of:
request({options})
.then(function(result){...})
.catch(function(error){...})
但是,我希望能够使用Promise.all并尝试同时进行多个呼叫,并等待它们全部解决,然后再进行其他呼叫.例如,我想:
However I want to be able to use Promise.all and try to make multiple calls at the same time and wait for them to all resolve and then proceed with other calls. For example I want to:
- 呼叫创建一个用户的一个应用程序.
- 同时拨打电话创建地址.
- Promise.all([UserCall,AddressCall]).then({处理结果的函数])?
我也一直在module.exports = {...}中使用我的函数.这是否要求我不在出口范围内,并让它们声明为单独的变量?
Also I have been working with my function in module.exports = {...}. Does this require me to be outside of exports and have them declare as separate variables?
据我了解,似乎必须做以下事情:
From what I understand its seems as if I have to do something like:
var UserCall = function(req,res){
return new Promise(function (resolve, reject){
request({options})? //To make the call to create a new user?
// Then something with resolve and reject
我们非常感谢您的帮助.我想我可能会混淆基本的BlueBird概念,并尝试将其与请求承诺一起使用.
Any help is much appreciated. I think I may be mixing up basic BlueBird concepts and trying to use them with request-promise.
推荐答案
在这里:
var BPromise = require('bluebird');
var rp = require('request-promise');
BPromise.all([
rp(optionsForRequest1),
rp(optionsForRequest2)
])
.spread(function (responseRequest1, responseRequest2) {
// Proceed with other calls...
})
.catch(function (err) {
// Will be called if at least one request fails.
});
这篇关于使用BlueBird/Request-Promise链接请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!