我正在尝试使用不同的网址发送两个调用(它们不是并发调用,第一个调用将首先执行,然后第二个调用可以执行)。我正在使用 Node 8 并使用 request-promise 库来执行此操作。 (一切都发生在谷歌云功能中)。问题是,当只有一个 url 要执行时,它工作得很好。但是当我添加第二个时,现在两个发布请求都不起作用。
这是我的代码:
const confirmSms = {
method: 'POST',
uri: '.....',
body: {
quantity
},
json: true
};
const options = {
method: 'POST',
uri:`....`,
body: {
amount
},
json: true
};
而且,我也在使用 async/await。现在,我是如何执行的,这是代码:
For the first one :
await rp(confirmSms)
For Second One :
await rp(options)
我这样做正确吗?出了什么问题,数量和金额是从请求中获取的(因此该值肯定可用)。我可以使用不同的名称还是应该只使用“选项”?请帮忙
最佳答案
您可以使用请求 promise https://www.npmjs.com/package/request-promise
var request = require('request-promise');
var baseUrl = 'http://base_url';
request.post(baseUrl + '/url_01')
.then(function(body) {
console.log("data 1: " + body);
}).then(function() {
return request.post(baseUrl + '/url_2'); // <-- inside a function
}).then(function(body) {
console.log("data 2: " + body);
}).catch(function (err) {
console.error(err);
});
关于javascript - 使用 request-promise 发送两个 post 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55232933/