如何从 resolve() 和 reject() 中获取多个值?
How to get multiple values out of resolve() and reject()?
我想要 resolve()
到 return {valid_to: cert.valid_to, statusCode, statusMessage}
和 reject()
应该 return {error: -1, statusCode, statusMessage}
.
问题
当 statusCode
、statusMessage
处于不同的范围时,我该怎么做?
const https = require('https');
(async () => {
const options = {
hostname: "github.com",
port: 443,
path: '/',
method: 'GET',
timeout: 1000
};
options.agent = new https.Agent(options);
let valid_to = await new Promise((resolve, reject) => {
const req = https.request({
...options, checkServerIdentity: function (host, cert) {
resolve(cert.valid_to);
}
}).on('error', error => {
reject(-2);
});
req.on("timeout", chunk => {
reject(-1);
});
req.on('response', response => {
console.log(response.statusCode);
console.log(response.statusMessage);
});
req.end();
}).catch(error => {
console.log(error);
return -3;
});
})();
我会做这样的事情。
编辑:您需要在 https.request
对象中指定 res.on('data')
。否则,超时将始终发出,因为流中没有 activity。
您可以在 res.on("data")
或 res.on("end")
中解决,这取决于您的用例。
res
是 an IncomingMessage object is created by http.ClientRequest 并分别作为第一个参数传递给 'request' 和 'response' 事件。
req
是 A reference to the original http.ClientRequest.
两个流都可以发出事件,您可以分别处理它们。
还有,当你reject Promise的时候,你实际上是无法从req
获取statusCode和StatusMessage的,因为req
有错误,.on("response")
不会发射。所以,你需要自己定制statusCode
和statusMessage
。
const https = require("https");
// {valid_to: cert.valid_to, statusCode, statusMessage}
// {error: -1, statusCode, statusMessage}.
(async () => {
const options = {
hostname: "githubasdfa.com",
port: 443,
path: "/",
method: "GET",
timeout: 1000,
};
options.agent = new https.Agent(options);
try {
const response = await new Promise((resolve, reject) => {
let valid_to;
let statusCode;
let statusMessage;
const req = https
.request(
{
...options,
checkServerIdentity: function (host, cert) {
valid_to = cert.valid_to;
},
},
res => {
res.on("data", chunk => {
resolve({
valid_to,
statusCode,
statusMessage,
});
});
res.on("end", () => {
console.log("No more data in response.");
});
}
)
.on("error", err => {
console.log(err);
reject({
error: -2,
statusCode: "custom code",
statusMessage: "unhandled error",
});
})
.on("timeout", chunk => {
reject({
error: -1,
statusCode: "custom code",
statusMessage: "unhandled error",
});
})
.on("response", response => {
statusCode = response.statusCode;
statusMessage = response.statusMessage;
})
.end();
});
console.log(response);
} catch (error) {
console.log(error);
}
})();
我想要 resolve()
到 return {valid_to: cert.valid_to, statusCode, statusMessage}
和 reject()
应该 return {error: -1, statusCode, statusMessage}
.
问题
当 statusCode
、statusMessage
处于不同的范围时,我该怎么做?
const https = require('https');
(async () => {
const options = {
hostname: "github.com",
port: 443,
path: '/',
method: 'GET',
timeout: 1000
};
options.agent = new https.Agent(options);
let valid_to = await new Promise((resolve, reject) => {
const req = https.request({
...options, checkServerIdentity: function (host, cert) {
resolve(cert.valid_to);
}
}).on('error', error => {
reject(-2);
});
req.on("timeout", chunk => {
reject(-1);
});
req.on('response', response => {
console.log(response.statusCode);
console.log(response.statusMessage);
});
req.end();
}).catch(error => {
console.log(error);
return -3;
});
})();
我会做这样的事情。
编辑:您需要在 https.request
对象中指定 res.on('data')
。否则,超时将始终发出,因为流中没有 activity。
您可以在 res.on("data")
或 res.on("end")
中解决,这取决于您的用例。
res
是 an IncomingMessage object is created by http.ClientRequest 并分别作为第一个参数传递给 'request' 和 'response' 事件。
req
是 A reference to the original http.ClientRequest.
两个流都可以发出事件,您可以分别处理它们。
还有,当你reject Promise的时候,你实际上是无法从req
获取statusCode和StatusMessage的,因为req
有错误,.on("response")
不会发射。所以,你需要自己定制statusCode
和statusMessage
。
const https = require("https");
// {valid_to: cert.valid_to, statusCode, statusMessage}
// {error: -1, statusCode, statusMessage}.
(async () => {
const options = {
hostname: "githubasdfa.com",
port: 443,
path: "/",
method: "GET",
timeout: 1000,
};
options.agent = new https.Agent(options);
try {
const response = await new Promise((resolve, reject) => {
let valid_to;
let statusCode;
let statusMessage;
const req = https
.request(
{
...options,
checkServerIdentity: function (host, cert) {
valid_to = cert.valid_to;
},
},
res => {
res.on("data", chunk => {
resolve({
valid_to,
statusCode,
statusMessage,
});
});
res.on("end", () => {
console.log("No more data in response.");
});
}
)
.on("error", err => {
console.log(err);
reject({
error: -2,
statusCode: "custom code",
statusMessage: "unhandled error",
});
})
.on("timeout", chunk => {
reject({
error: -1,
statusCode: "custom code",
statusMessage: "unhandled error",
});
})
.on("response", response => {
statusCode = response.statusCode;
statusMessage = response.statusMessage;
})
.end();
});
console.log(response);
} catch (error) {
console.log(error);
}
})();