如何用承诺来链接和分享先前的结果
callhttp()
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]);});
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 ); } });}
慕的地10843
相关分类