如何用承諾來鏈接和分享先前的結(jié)果我正在使用Bluebird庫,需要發(fā)出一系列HTTP請求,并需要對下一個HTTP請求的一些響應(yīng)數(shù)據(jù)進(jìn)行處理。我構(gòu)建了一個函數(shù)來處理我的請求callhttp()..這需要一個網(wǎng)址和一個帖子的正文。我這樣叫它:var payload = '{"Username": "joe", "Password": "password"}';var join = Promise.join;join(
callhttp("172.16.28.200", payload),
callhttp("172.16.28.200", payload),
callhttp("172.16.28.200", payload),
function (first, second, third) {
console.log([first, second, third]);});第一個請求獲得一個API密鑰,需要傳遞給第二個請求,依此類推。如何從第一個請求獲得響應(yīng)數(shù)據(jù)?更新這是callhttp職能:var Promise = require("bluebird");var Request = Promise.promisify(require('request'));function callhttp(host, body) {
var options = {
url: 'https://' + host + '/api/authorize',
method: "POST",
headers: {
'content-type': 'application/json'
},
body: body,
strictSSL: false
};
return Request(options).spread(function (response) {
if (response.statusCode == 200) {
// console.log(body)
console.log(response.connection.getPeerCertificate().subject.CN)
return {
data: response.body };
} else {
// Just an example, 200 is not the only successful code
throw new Error("HTTP Error: " + response.statusCode );
}
});}
如何用承諾來鏈接和分享先前的結(jié)果
慕萊塢森
2019-06-19 19:49:20