Use timeout in getWeather

This commit is contained in:
SileNce5k 2023-12-06 17:40:56 +01:00
parent c4f526d37f
commit 3927b7905b
No known key found for this signature in database
GPG key ID: 961132EB78C8915F

View file

@ -1,10 +1,17 @@
const https = require('https');
module.exports = async function (location) {
const options = {
hostname: "wttr.in",
port: 443,
path: `${location}?format=4&M`,
method: 'GET',
timeout: 5000
}
const url = `https://wttr.in/${location}?format=4&M` // 4 = one line, M = metric wind speed
let success = false;
let weather = await new Promise((resolve, reject) => {
https.get(url, (res) => {
https.get(options, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
@ -24,7 +31,10 @@ module.exports = async function (location) {
});
}).on("error", (err) => {
reject(err);
}).on('timeout', (err) => {
reject(err)
});
})
return {success: success, weather: weather};