1

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>
Mr. Oleg
  • 37
  • 3
  • 1
    The value you want won't exist until *the future*; the function has to return *now*; what you want is impossible. – Quentin Mar 17 '21 at 19:44

0 Answers0