diff --git a/assets/scripts/main.js b/assets/scripts/main.js index d5d5f65b..8b8f6d18 100644 --- a/assets/scripts/main.js +++ b/assets/scripts/main.js @@ -68,15 +68,33 @@ async function getRecipes() { // EXPOSE - START (All expose numbers start with A) // A1. TODO - Check local storage to see if there are any recipes. // If there are recipes, return them. + const existRecipes = JSON.parse(localStorage.getItem('recipes')); /**************************/ // The rest of this method will be concerned with requesting the recipes // from the network // A2. TODO - Create an empty array to hold the recipes that you will fetch + let requestRecipes = []; + if(existRecipes != null){ + return existRecipes; + } // A3. TODO - Return a new Promise. If you are unfamiliar with promises, MDN // has a great article on them. A promise takes one parameter - A // function (we call these callback functions). That function will // take two parameters - resolve, and reject. These are functions // you can call to either resolve the Promise or Reject it. + return new Promise(async (resolve, reject)=>{ + for(let recipesFromURL of RECIPE_URLS){ + try{ + console.log(await (await fetch(recipesFromURL)).json()); + requestRecipes.push (await (await fetch(recipesFromURL)).json()); + }catch(err){ + console.log(err); + reject(err); + } + } + saveRecipesToStorage(requestRecipes); + resolve(requestRecipes); + }) /**************************/ // A4-A11 will all be *inside* the callback function we passed to the Promise // we're returning diff --git a/sw.js b/sw.js index bb179ca1..f74cc7be 100644 --- a/sw.js +++ b/sw.js @@ -2,7 +2,6 @@ // so do not move it next to the other scripts const CACHE_NAME = 'lab-7-starter'; - // Installs the service worker. Feed it some initial URLs to cache self.addEventListener('install', function (event) { event.waitUntil( @@ -20,7 +19,7 @@ self.addEventListener('activate', function (event) { }); // Intercept fetch requests and cache them -self.addEventListener('fetch', function (event) { +// self.addEventListener('fetch', async (event) => { // We added some known URLs to the cache above, but tracking down every // subsequent network request URL and adding it manually would be very taxing. // We will be adding all of the resources not specified in the intiial cache @@ -37,4 +36,4 @@ self.addEventListener('fetch', function (event) { // B8. TODO - If the request is in the cache, return with the cached version. // Otherwise fetch the resource, add it to the cache, and return // network response. -}); +// }); \ No newline at end of file