gpt4 book ai didi

javascript - 使用 BlueBird/Request-Promise 链接请求

转载 作者:行者123 更新时间:2023-11-27 22:48:12 24 4
gpt4 key购买 nike

我正在使用请求 promise 模块,但没有发现任何提及如何链接请求的内容。我目前正在遵循他们的语法:

request({options})
.then(function(result){...})
.catch(function(error){...})

但是我希望能够使用 Promise.all 并尝试同时进行多个调用并等待它们全部解决,然后继续进行其他调用。例如我想要:

  1. 调用一个应用来创建用户。
  2. 同时调用电话创建地址。
  3. Promise.all([UserCall, AddressCall]).then({处理结果的函数])?

此外,我一直在 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 一起使用。

最佳答案

给你:

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.
});

关于javascript - 使用 BlueBird/Request-Promise 链接请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38257116/

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