Is this a realistic example of using async await?

Hey,

Is this a realistic example of async await or is it just showing how the timing works?

Thanks

let stocks = {
  Fruits: ["strawberry", "grapes", "banana", "apple"],
  liquid: ["water", "ice"],
  holder: ["cone", "cup", "stick"],
  toppings: ["chocolate", "peanuts"]
};

let is_shop_open = true;

function time(ms) {
  return new Promise((resolve, reject) => {
    if (is_shop_open) {
      setTimeout(resolve, ms);
    } else {
      reject(console.log("The shop is closed"));
    }
  });
}

async function kitchen() {
  try {
    await time(2000);
    console.log(`The customer ordered ${stocks.Fruits[0]} ice cream.`);

    await time(0);
    console.log("Start the production.");

    await time(2000);
    console.log("Cut the ${stocks.Fruit[0]}");

    await time(1000);
    console.log(`${stocks.liquid[0]} and ${stocks.liquid[1]} was added`);

    await time(1000);
    console.log("Start the machine.");

    await time(2000);
    console.log(`The ice cream was placed on the ${stocks.holder[0]}`);

    await time(3000);
    console.log(`The ${stocks.toppings[0]} was added.`);

    await time(3000);
    console.log("The ice cream has been served");
    
  } catch (error) {
    console.log("The customer left", error);
  } finally {
    console.log("The day is over. Shop is closed.");
  }
}

kitchen();

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.