Skip to content
Open
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
163 changes: 143 additions & 20 deletions javascript/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,160 @@
// 🚨🚨🚨 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 += `<li>${step1}</li>`;
}, (error) => console.log(error));

getInstruction("mashedPotatoes", 1, (step2) => {
},
(error) => console.log(error)
);

getInstruction(
"mashedPotatoes",
1,
(step2) => {
document.querySelector("#mashedPotatoes").innerHTML += `<li>${step2}</li>`;
}, (error) => console.log(error));

getInstruction("mashedPotatoes", 2, (step3) => {
},
(error) => console.log(error)
);

getInstruction(
"mashedPotatoes",
2,
(step3) => {
document.querySelector("#mashedPotatoes").innerHTML += `<li>${step3}</li>`;
}, (error) => console.log(error));

getInstruction("mashedPotatoes", 3, (step4) => {
},
(error) => console.log(error)
);

getInstruction(
"mashedPotatoes",
3,
(step4) => {
document.querySelector("#mashedPotatoes").innerHTML += `<li>${step4}</li>`;
}, (error) => console.log(error));

getInstruction("mashedPotatoes", 4, (step5) => {
},
(error) => console.log(error)
);

getInstruction(
"mashedPotatoes",
4,
(step5) => {
document.querySelector("#mashedPotatoes").innerHTML += `<li>${step5}</li>`;
document.querySelector("#mashedPotatoesImg").removeAttribute("hidden");
}, (error) => console.log(error));


},
(error) => console.log(error)
);

// Iteration 1 - using callbacks
// ...
getInstruction("mashedPotatoes", 0, (step0) => {
document.querySelector("#mashedPotatoes").innerHTML += `<li>${step0}</li>`;
// ... Your code here
});

// Iteration 2 - using promises
// ...
obtainInstruction("steak", 0)
.then((step0) => {
document.querySelector("#steak").innerHTML += `<li>${step0}</li>`;
obtainInstruction("steak", 1).then((step1) => {
document.querySelector("#steak").innerHTML += `<li>${step1}</li>`;
obtainInstruction("steak", 2).then((step2) => {
document.querySelector("#steak").innerHTML += `<li>${step2}</li>`;
obtainInstruction("steak", 3).then((step3) => {
document.querySelector("#steak").innerHTML += `<li>${step3}</li>`;
obtainInstruction("steak", 4).then((step4) => {
document.querySelector("#steak").innerHTML += `<li>${step4}</li>`;
obtainInstruction("steak", 5).then((step5) => {
document.querySelector("#steak").innerHTML += `<li>${step5}</li>`;
obtainInstruction("steak", 6).then((step6) => {
document.querySelector(
"#steak"
).innerHTML += `<li>${step6}</li>`;
obtainInstruction("steak", 7)
.then((step7) => {
document.querySelector(
"#steak"
).innerHTML += `<li>${step7}</li>`;
})
.then(() => {
document.querySelector(
"#steak"
).innerHTML += `<li>Stake is ready!</li>`;
document
.querySelector("#steakImg")
.removeAttribute("hidden");
});
});
});
});
});
});
});
})
.catch((error) => console.log(error));

// Iteration 3 using async/await
// ...
async function makeBroccoli() {
// ... Your code here
try {
await obtainInstruction("broccoli", 0).then(
(step0) =>
(document.querySelector("#broccoli").innerHTML += `<li>${step0}</li>`)
);
await obtainInstruction("broccoli", 1).then(
(step1) =>
(document.querySelector("#broccoli").innerHTML += `<li>${step1}</li>`)
);
await obtainInstruction("broccoli", 2).then(
(step2) =>
(document.querySelector("#broccoli").innerHTML += `<li>${step2}</li>`)
);
await obtainInstruction("broccoli", 3).then(
(step3) =>
(document.querySelector("#broccoli").innerHTML += `<li>${step3}</li>`)
);
await obtainInstruction("broccoli", 4).then(
(step4) =>
(document.querySelector("#broccoli").innerHTML += `<li>${step4}</li>`)
);
await obtainInstruction("broccoli", 5).then(
(step5) =>
(document.querySelector("#broccoli").innerHTML += `<li>${step5}</li>`)
);
await obtainInstruction("broccoli", 6).then(
(step6) =>
(document.querySelector("#broccoli").innerHTML += `<li>${step6}</li>`)
);
document.querySelector(
"#broccoli"
).innerHTML += `<li>Broccoli is ready!</li>`;
document.querySelector("#broccoliImg").removeAttribute("hidden");
} catch (error) {
console.log(error);
}
}

makeBroccoli();

// Bonus 2 - Promise all
// ...
const step0 = obtainInstruction("brusselsSprouts", 0);
const step1 = obtainInstruction("brusselsSprouts", 1);
const step2 = obtainInstruction("brusselsSprouts", 2);
const step3 = obtainInstruction("brusselsSprouts", 3);
const step4 = obtainInstruction("brusselsSprouts", 4);
const step5 = obtainInstruction("brusselsSprouts", 5);
const step6 = obtainInstruction("brusselsSprouts", 6);
const step7 = obtainInstruction("brusselsSprouts", 7);
const step8 = "Brussels sprouts are ready!";
Promise.all([step0, step1, step2, step3, step4, step5, step6, step7, step8])
.then((result) => {
result.map(
(step) =>
(document.querySelector(
"#brusselsSprouts"
).innerHTML += `<li>${step}</li>`)
);
document.querySelector("#brusselsSproutsImg").removeAttribute("hidden");
})
.catch((error) => console.log(error));