diff --git a/javascript/index.js b/javascript/index.js index 9f30552db..ceade24ff 100644 --- a/javascript/index.js +++ b/javascript/index.js @@ -26,15 +26,87 @@ }, (error) => console.log(error)); + // Iteration 1 - using callbacks + getInstruction('mashedPotatoes', 0, (step0) => { + document.querySelector("#mashedPotatoes").innerHTML += `
  • ${step0}
  • `; + getInstruction('mashedPotatoes', 1, (step1) => { + document.querySelector("#mashedPotatoes").innerHTML += `
  • ${step1}
  • `; + getInstruction('mashedPotatoes', 2, (step2) => { + document.querySelector("#mashedPotatoes").innerHTML += `
  • ${step2}
  • `; + getInstruction('mashedPotatoes', 3, (step3) => { + document.querySelector("#mashedPotatoes").innerHTML += `
  • ${step3}
  • `; + getInstruction('mashedPotatoes', 4, (step4) => { + document.querySelector("#mashedPotatoes").innerHTML += `
  • ${step4}
  • `; + document.querySelector("#mashedPotatoesImg").removeAttribute("hidden"); + }, (error) => console.log(error)); + }, (error) => console.log(error)); + }, (error) => console.log(error)); + }, (error) => console.log(error)); + }, (error) => console.log(error)); -// Iteration 1 - using callbacks -// ... + // Iteration 2 - using promises + obtainInstruction('steak', 0) + .then((step0) => { + document.querySelector("#steak").innerHTML += `
  • ${step0}
  • `; + return obtainInstruction('steak', 1); + }) + .then((step1) => { + document.querySelector("#steak").innerHTML += `
  • ${step1}
  • `; + return obtainInstruction('steak', 2); + }) + .then((step2) => { + document.querySelector("#steak").innerHTML += `
  • ${step2}
  • `; + return obtainInstruction('steak', 3); + }) + .then((step3) => { + document.querySelector("#steak").innerHTML += `
  • ${step3}
  • `; + return obtainInstruction('steak', 4); + }) + .then((step4) => { + document.querySelector("#steak").innerHTML += `
  • ${step4}
  • `; + document.querySelector("#steakImg").removeAttribute("hidden"); + }) + .catch((error) => console.log(error)); -// Iteration 2 - using promises -// ... + // Iteration 3 using async/await + async function makeBroccoli() { + try { + const step0 = await obtainInstruction('broccoli', 0); + document.querySelector("#broccoli").innerHTML += `
  • ${step0}
  • `; + + const step1 = await obtainInstruction('broccoli', 1); + document.querySelector("#broccoli").innerHTML += `
  • ${step1}
  • `; + + const step2 = await obtainInstruction('broccoli', 2); + document.querySelector("#broccoli").innerHTML += `
  • ${step2}
  • `; + + const step3 = await obtainInstruction('broccoli', 3); + document.querySelector("#broccoli").innerHTML += `
  • ${step3}
  • `; + + const step4 = await obtainInstruction('broccoli', 4); + document.querySelector("#broccoli").innerHTML += `
  • ${step4}
  • `; + + document.querySelector("#broccoliImg").removeAttribute("hidden"); + } catch (error) { + console.log(error); + } + } + makeBroccoli(); -// Iteration 3 using async/await -// ... + // Bonus 2 - Promise all + const brusselsSproutsPromises = [ + obtainInstruction('brusselsSprouts', 0), + obtainInstruction('brusselsSprouts', 1), + obtainInstruction('brusselsSprouts', 2), + obtainInstruction('brusselsSprouts', 3), + obtainInstruction('brusselsSprouts', 4) + ]; -// Bonus 2 - Promise all -// ... \ No newline at end of file + Promise.all(brusselsSproutsPromises) + .then((values) => { + values.forEach((instruction) => { + document.querySelector("#brusselsSprouts").innerHTML += `
  • ${instruction}
  • `; + }); + document.querySelector("#brusselsSproutsImg").removeAttribute("hidden"); + }) + .catch((error) => console.log(error)); \ No newline at end of file