Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Yahoo's branch #8

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions assets/scripts/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand All @@ -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.
});
// });