I have simple task: I have some menu, and I need to cook it, each dish has it's own timer. When everything will be ready, I have to receive an array, that the dishes from menu are ready, but I'm getting undefined, because return will be executed before setTimeout and I don't know how to fix it. Please help.
<script>
const menu = {
burger: [
{
name: '',
time: 2000
},
{
name: '',
time: 3500
},
{
name: '',
time: 3200
},
],
hotDog: [
{
name: '',
time: 500
},
{
name: '',
time: 500
},
{
name: '',
time: 500
}],
pizza: [
{
name: '',
time: 800
},
{
name: '',
time: 800
}]
}
function order(menuName) {
console.log('Start cooking...');
const result = [];
const foo = (name) => {
result.push(name);
}
function cooking({name, time}) {
setTimeout(() => {
foo(name);
}, time);
};
menuName.forEach(cooking);
}
console.log(order(menu.burger));
</script>