From 3927b7905be86e0bc958aa9fec332b6a3dd975ed Mon Sep 17 00:00:00 2001 From: SileNce5k Date: Wed, 6 Dec 2023 17:40:56 +0100 Subject: [PATCH] Use timeout in getWeather --- util/getWeather.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/util/getWeather.js b/util/getWeather.js index cce7049..a774c9b 100644 --- a/util/getWeather.js +++ b/util/getWeather.js @@ -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};