diff --git a/javascript/index.js b/javascript/index.js index 9f30552db..afdfe1974 100644 --- a/javascript/index.js +++ b/javascript/index.js @@ -4,7 +4,7 @@ // 🚨🚨🚨 Comment out the below code before you start working on the code // Out of sync - getInstruction("mashedPotatoes", 0, (step1) => { + /* getInstruction("mashedPotatoes", 0, (step1) => { document.querySelector("#mashedPotatoes").innerHTML += `
  • ${step1}
  • `; }, (error) => console.log(error)); @@ -25,16 +25,144 @@ document.querySelector("#mashedPotatoesImg").removeAttribute("hidden"); }, (error) => console.log(error)); - +*/ // Iteration 1 - using callbacks -// ... + +function onError(err) { + console.error(err); +} + +const mashedPotatoesList = document.querySelector("#mashedPotatoes"); + +getInstruction("mashedPotatoes", 0, (step0) => { + mashedPotatoesList.innerHTML += `
  • ${step0}
  • `; + + getInstruction("mashedPotatoes", 1, (step1) => { + mashedPotatoesList.innerHTML += `
  • ${step1}
  • `; + + getInstruction("mashedPotatoes", 2, (step2) => { + mashedPotatoesList.innerHTML += `
  • ${step2}
  • `; + + getInstruction("mashedPotatoes", 3, (step3) => { + mashedPotatoesList.innerHTML += `
  • ${step3}
  • `; + + getInstruction("mashedPotatoes", 4, (step4) => { + mashedPotatoesList.innerHTML += `
  • ${step4}
  • `; + mashedPotatoesList.innerHTML += `
  • Mashed potatoes are ready!
  • `; + const img = document.querySelector("#mashedPotatoesImg"); + if (img) img.removeAttribute("hidden"); + }, onError); + + }, onError); + + }, onError); + + }, onError); + +}, onError); + // Iteration 2 - using promises -// ... -// Iteration 3 using async/await -// ... +const steakList = document.querySelector("#steak"); + +obtainInstruction("steak", 0) + .then((step0) => { + steakList.innerHTML += `
  • ${step0}
  • `; + return obtainInstruction("steak", 1); + }) + .then((step1) => { + steakList.innerHTML += `
  • ${step1}
  • `; + return obtainInstruction("steak", 2); + }) + .then((step2) => { + steakList.innerHTML += `
  • ${step2}
  • `; + return obtainInstruction("steak", 3); + }) + .then((step3) => { + steakList.innerHTML += `
  • ${step3}
  • `; + return obtainInstruction("steak", 4); + }) + .then((step4) => { + steakList.innerHTML += `
  • ${step4}
  • `; + return obtainInstruction("steak", 5); + }) + .then((step5) => { + steakList.innerHTML += `
  • ${step5}
  • `; + return obtainInstruction("steak", 6); + }) + .then((step6) => { + steakList.innerHTML += `
  • ${step6}
  • `; + return obtainInstruction("steak", 7); + }) + .then((step7) => { + steakList.innerHTML += `
  • ${step7}
  • `; + steakList.innerHTML += `
  • Steak is ready!
  • `; + const img = document.querySelector("#steakImg"); + if (img) img.removeAttribute("hidden"); + }) + .catch((err) => console.error(err)); + + +// Iteration 3 - using async/await + +async function makeBroccoli() { + try { + const broccoliList = document.querySelector("#broccoli"); + + const step0 = await obtainInstruction("broccoli", 0); + broccoliList.innerHTML += `
  • ${step0}
  • `; + + const step1 = await obtainInstruction("broccoli", 1); + broccoliList.innerHTML += `
  • ${step1}
  • `; + + const step2 = await obtainInstruction("broccoli", 2); + broccoliList.innerHTML += `
  • ${step2}
  • `; + + const step3 = await obtainInstruction("broccoli", 3); + broccoliList.innerHTML += `
  • ${step3}
  • `; + + const step4 = await obtainInstruction("broccoli", 4); + broccoliList.innerHTML += `
  • ${step4}
  • `; + + const step5 = await obtainInstruction("broccoli", 5); + broccoliList.innerHTML += `
  • ${step5}
  • `; + + const step6 = await obtainInstruction("broccoli", 6); + broccoliList.innerHTML += `
  • ${step6}
  • `; + + broccoliList.innerHTML += `
  • Broccoli is ready!
  • `; + const img = document.querySelector("#broccoliImg"); + if (img) img.removeAttribute("hidden"); + } catch (err) { + console.error(err); + } +} + +makeBroccoli(); + + +// Bonus: Iteration 5 - Promise.all() + +const brusselsList = document.querySelector("#brusselsSprouts"); -// Bonus 2 - Promise all -// ... \ No newline at end of file +Promise.all([ + obtainInstruction("brusselsSprouts", 0), + obtainInstruction("brusselsSprouts", 1), + obtainInstruction("brusselsSprouts", 2), + obtainInstruction("brusselsSprouts", 3), + obtainInstruction("brusselsSprouts", 4), + obtainInstruction("brusselsSprouts", 5), + obtainInstruction("brusselsSprouts", 6), + obtainInstruction("brusselsSprouts", 7), +]) + .then((steps) => { + steps.forEach((step) => { + brusselsList.innerHTML += `
  • ${step}
  • `; + }); + brusselsList.innerHTML += `
  • Brussels sprouts are ready!
  • `; + const img = document.querySelector("#brusselsSproutsImg"); + if (img) img.removeAttribute("hidden"); + }) + .catch((err) => console.error(err)); \ No newline at end of file