@@ -7,56 +7,55 @@ const iconElement = document.querySelector('.weatherIcon');
77const tempElement = document . querySelector ( '.weatherValue p' ) ;
88const descElement = document . querySelector ( '.weatherDescription p' ) ;
99
10- const weather = { } ;
11- weather . temperature = {
12- unit : 'celsius' ,
13- } ;
14-
1510var tempUnit = CONFIG . weatherUnit ;
1611
1712const KELVIN = 273.15 ;
1813const key = `${ CONFIG . weatherKey } ` ;
19- setPosition ( ) ;
20-
21- function setPosition ( position ) {
22- if ( ! CONFIG . trackLocation || ! navigator . geolocation ) {
23- if ( CONFIG . trackLocation ) {
24- console . error ( 'Geolocation not available' ) ;
25- }
26- getWeather ( CONFIG . defaultLatitude , CONFIG . defaultLongitude ) ;
27- return ;
28- }
29- navigator . geolocation . getCurrentPosition (
30- pos => {
31- getWeather ( pos . coords . latitude . toFixed ( 3 ) , pos . coords . longitude . toFixed ( 3 ) ) ;
32- } ,
33- err => {
34- console . error ( err ) ;
35- getWeather ( CONFIG . defaultLatitude , CONFIG . defaultLongitude ) ;
36- }
37- ) ;
14+ const weatherPromise = getWeather ( ) ;
15+ displayWeather ( ) ;
16+
17+ async function setPosition ( ) {
18+ let pos ;
19+ if ( ! CONFIG . trackLocation || ! navigator . geolocation ) {
20+ if ( CONFIG . trackLocation ) {
21+ console . error ( 'Geolocation not available' ) ;
22+ }
23+ pos = ( { lat : CONFIG . defaultLatitude , lon : CONFIG . defaultLongitude } ) ;
24+ }
25+
26+ pos = new Promise ( ( resolve ) => {
27+ navigator . geolocation . getCurrentPosition ( ( pos ) => {
28+ resolve ( { lat : pos . coords . latitude . toFixed ( 3 ) , lon : pos . coords . longitude . toFixed ( 3 ) } ) ;
29+ } ,
30+ ( ) => {
31+ resolve ( { lat : CONFIG . defaultLatitude , lon : CONFIG . defaultLongitude } ) ;
32+ } ) ;
33+ } ) ;
34+ return pos ;
3835}
3936
40- function getWeather ( latitude , longitude ) {
41- let api = `https://api.openweathermap.org/data/2.5/weather?lat=${ latitude } &lon=${ longitude } &lang=${ CONFIG . language } &appid=${ key } ` ;
42- fetch ( api )
43- . then ( function ( response ) {
44- let data = response . json ( ) ;
45- return data ;
46- } )
47- . then ( function ( data ) {
48- let celsius = Math . floor ( data . main . temp - KELVIN ) ;
49- weather . temperature . value = tempUnit == 'C' ? celsius : ( celsius * 9 ) / 5 + 32 ;
50- weather . description = data . weather [ 0 ] . description ;
51- weather . iconId = data . weather [ 0 ] . icon ;
52- } )
53- . then ( function ( ) {
54- displayWeather ( ) ;
55- } ) ;
37+ async function getWeather ( ) {
38+ const position = await setPosition ( ) ;
39+ let api = `https://api.openweathermap.org/data/2.5/weather?lat=${ position . lat } &lon=${ position . lon } &lang=${ CONFIG . language } &appid=${ key } ` ;
40+
41+ let response = await fetch ( api ) . catch ( err => {
42+ console . log ( err ) ;
43+ } ) ;
44+ let data = await response . json ( ) ;
45+
46+ let celsius = Math . floor ( data . main . temp - KELVIN ) ;
47+ return {
48+ description : data . weather [ 0 ] . description ,
49+ iconId : data . weather [ 0 ] . icon ,
50+ sunrise : data . sys . sunrise ,
51+ sunset : data . sys . sunset ,
52+ temperature : tempUnit == 'C' ? celsius : ( celsius * 9 ) / 5 + 32
53+ } ;
5654}
5755
58- function displayWeather ( ) {
59- iconElement . innerHTML = `<img src="assets/icons/${ CONFIG . weatherIcons } /${ weather . iconId } .png"/>` ;
60- tempElement . innerHTML = `${ weather . temperature . value . toFixed ( 0 ) } Β°<span class="darkfg">${ tempUnit } </span>` ;
61- descElement . innerHTML = weather . description ;
56+ async function displayWeather ( ) {
57+ var weather = await weatherPromise ;
58+ iconElement . innerHTML = `<img src="assets/icons/${ CONFIG . weatherIcons } /${ weather . iconId } .png"/>` ;
59+ tempElement . innerHTML = `${ weather . temperature . toFixed ( 0 ) } Β°<span class="darkfg">${ tempUnit } </span>` ;
60+ descElement . innerHTML = weather . description ;
6261}
0 commit comments