Use timeout in getWeather
This commit is contained in:
parent
c4f526d37f
commit
3927b7905b
1 changed files with 11 additions and 1 deletions
|
@ -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};
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue