我正在使用request-promise模块并且未发现如何链接请求。我目前正在跟随他们的语法:使用BlueBird/Request-Promise的链接请求
request({options})
.then(function(result){...})
.catch(function(error){...})
不过,我希望能够用Promise.all并试图在同一时间,使多个呼叫,等待他们所有的决心,然后用其他电话进行。例如,我想要:
- 拨打一个创建用户的应用程序。
- 在同一时间,拨打电话创建一个地址。
- Promise.all([UserCall,AddressCall])。then({function to deal with results])?
此外,我一直在使用我的功能module.exports = {...}。这是否要求我在出口之外并让他们声明为单独的变量?
从我的理解它好像我必须做这样的事情:
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概念并尝试将它们用于请求承诺。
是的,就用'Promise.all([请求({...}),请求({...})])'。究竟是什么问题?你有什么尝试? – Bergi
不,你不应该使用'new Promise'构造函数,如果request(...)'已经返回一个承诺 – Bergi
我尝试使用'Promise.all([request({...}),request({...})])。然后(函数(结果){...})'。但由于某种原因,我收到了2个请求中的空值或未定义结果。我做了一些控制台日志,好像.then函数没有等待结果返回。我是否应该为每个'request({...})'放置'.then(function(result){return result})? – Jeff