下面的代码有效,但是我想在最后query()
仅基于返回0
或1
return (statusCode === 301 && httpsGet.includes('google')) ? 1 : 0
可能会引入一个全局变量,但是它接错了。
如何在res.statusCode
语句中访问try
?
const https = require('https');
function httpsGet(opts) {
return new Promise((resolve, reject) => {
const req = https.get(opts, (res) => {
let body = '';
res.on('data', (data) => {
body += data.toString();
});
console.log(res.statusCode);
res.on('end', () => {
resolve(body);
});
});
req.on('error', (e) => {
console.error(e);
reject(e);
});
});
}
async function query(options) {
options = options || {};
options.timeout = options.timeout || 0;
const opt = {
hostname: 'encrypted.google.com',
port: 443,
path: '/',
method: 'GET',
};
opt.agent = new https.Agent(opt);
try {
// here I would like to check the status code and search in httpsGet return value
return await httpsGet(opt);
} catch (error) {
console.error(error);
}
}
(async function () {
const s = await query({ timeout: 1000 });
console.log('String: ' + s);
})();
答案 0 :(得分:3)
返回的值就是您传递给resolve函数的任何值。
因此,如果您需要状态码,则可以将其添加到返回的对象中。
private enum enRegistersAddr
{
Volt = 0,
Current = 20,
Kw = 140
}
private void Connect_Click(object sender, EventArgs e)
{
button1WasClicked = true;
try
{
byte slaveAddress = byte.Parse(ConnectionSetUp.SetValueForAdress);
IModbusMaster masterRTU = ModbusSerialMaster.CreateRtu(serialPort);
ushort numOfPoints = 3;
ushort[] result = masterRTU.ReadHoldingRegisters (slaveAddress,
(ushort)enRegistersAddr.Volt, numOfPoints);
//display result.
VoltR.Text = string.Empty;
VoltR.Text = string.Format("{0}", result[0]);
VoltS.Text = string.Format("{0}", result[1]);
VoltT.Text = string.Format("{0}", result[2]);
result = masterRTU.ReadHoldingRegisters (slaveAddress,
(ushort)enRegistersAddr.Current, numOfPoints);
CurrentR.Text = string.Format("{0}", result[0]);
CurrentS.Text = string.Format("{0}", result[1]);
CurrentT.Text = string.Format("{0}", result[2]);
result = masterRTU.ReadHoldingRegisters (slaveAddress,
(ushort)enRegistersAddr.Kw, numOfPoints);
Kw_R.Text = string.Format("{0}", result[0]);
Kw_S.Text = string.Format("{0}", result[1]);
Kw_T.Text = string.Format("{0}", result[2]);
}
catch (Exception ex)
{
timer1.Stop();
MessageBox.Show(this, ex.Message, "ERROR", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
如果您不熟悉解构,那么这两个是相同的:
function httpsGet(opts) {
return new Promise((resolve, reject) => {
const req = https.get(opts, (res) => {
let body = '';
res.on('data', (data) => {
body += data.toString();
});
console.log(res.statusCode);
res.on('end', () => {
resolve({
body,
statusCode: res.statusCode, // Here we add status code
});
});
});
req.on('error', (e) => {
console.error(e);
reject(e);
});
});
}
async function query(options) {
options = options || {};
options.timeout = options.timeout || 0;
const opt = {
hostname: 'encrypted.google.com',
port: 443,
path: '/',
method: 'GET',
};
opt.agent = new https.Agent(opt);
try {
// Unpack return value
const { body, statusCode } = await httpsGet(opt);
// Use values
return (statusCode === 301 && body.includes('google')) ? 1 : 0
} catch (error) {
console.error(error);
}
}
(async function () {
const s = await query({ timeout: 1000 });
console.log('String: ' + s);
})();
或
// Unpack return value
const { body, statusCode } = await httpsGet(opt);
// Use values
return (statusCode === 301 && body.includes('google')) ? 1 : 0