Advertisement
JPDG13

Asynchronous JavaScript examples in HTML

May 5th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.28 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7.     <title>Asynchronous JavaScript</title>
  8. </head>
  9. <body>
  10.     <h1>Asynchronous JavaScript</h1>
  11.     <script>
  12.        /*
  13.         async function getRecipesAW() {
  14.             const IDs = await getIDs;
  15.             console.log(IDs);
  16.             const recipe = await getRecipes(IDs[2]);
  17.             console.log(recipe);
  18.             const related = await getRelated('Jay DeGraaf');
  19.             console.log(related);
  20.            
  21.             return recipe;
  22.         }
  23.        
  24.         getRecipesAW().then.(result => console.log(`${result}` is the best recipe ever!));
  25.         */
  26.        
  27.         function getWeather(woeid) {
  28.         // cross origin allows you to access a site that is unaccessible.
  29.         fetch ('https://cors-anywhere.herokuapp.com/https://www.metaweather.com/api/location/${woeid}/')
  30.         .then(result => {
  31.             console.log(result);
  32.             return result.json();
  33.         })
  34.         .then(data => {
  35.             const today =data.consolidated_weather[0];
  36.             console.log(`Temperatures in ${data.title} stay between ${today.min_temp} and ${today.max_temp}.`)
  37.         })
  38.         .catch(error => console.log(error));  
  39.         }
  40.        
  41.         getWeather(2487956);
  42.         getWeather(44418);
  43.        
  44.        
  45.        
  46.         async function getWeatherAW (woeid) {
  47.            
  48.             try {
  49.                 const result = await fetch ('https://cors-anywhere.herokuapp.com/https://www.metaweather.com/api/location/${woeid}/')
  50.                 const data = await result.json();
  51.                 const tomorrow =data.consolidated_weather[1];
  52.            
  53.                  console.log(`Temperatures tomorrow in ${data.title} stay between ${tomorrow.min_temp} and ${today.max_temp}.`);
  54.                  return data;
  55.             } catch (error) {
  56.                 console.log(error);
  57.             }
  58.        
  59.         }
  60.        
  61.         getWeather(2487956);
  62.         let dataLondon;
  63.         getWeather(44418).then(data => {
  64.             dataLondon = data;
  65.             console.log(dataLondon);
  66.         });
  67.        
  68.  
  69.     </script>
  70. </body>
  71. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement