Async In JavaScript: The wedding planner

Let’s create an interesting example of the challenges faced by Julie, our wedding planner, who must coordinate the arrival of food and decorations using asynchronous functions in JavaScript, follow the steps below.

Step 1: Set Up the Project

1. **Install Node.js and NPM:** - Download and install Node.js from [Node.js official website]( This will also install npm (Node Package Manager).
2. **Create a New Project Directory:** - Open your terminal (Command Prompt, PowerShell, or Terminal on macOS/Linux) and create a new directory for your project. mkdir wedding-planner cd wedding-planner ```
3. **Initialize a New NPM Project:** - Run the following command to initialize a new NPM project: npm init -y ```
4. **Install Axios for HTTP Requests:** - Axios is a popular NPM package for making HTTP requests. ```bash npm install axios

Step 2: Write the JavaScript Code

Create a file named `index.js` in your project directory and write the following code:
const axios = require('axios');
// Simulating an API call to check food arrival status async function fetchFoodArrivalStatus() { console.log('Checking food arrival status...'); await new Promise(resolve => setTimeout(resolve, Math.random() * 2000 + 1000)); // Simulating network delay const status = Math.random() > 0.5 ? 'Food has arrived!' : 'Food is delayed!'; return status; }
// Simulating an API call to check decoration setup status async function fetchDecorationSetupStatus() { console.log('Checking decoration setup status...'); await new Promise(resolve => setTimeout(resolve, Math.random() * 2000 + 1000)); // Simulating network delay const status = Math.random() > 0.5 ? 'Decorations are ready!' : 'Decorations are delayed!'; return status; }
// Main function to coordinate the arrival of food and decorations async function coordinateWedding() { try { console.log('Starting the coordination process...');
// Fetching statuses concurrently const foodStatusPromise = fetchFoodArrivalStatus(); const decorationStatusPromise = fetchDecorationSetupStatus();
// Awaiting the results const foodStatus = await foodStatusPromise; const decorationStatus = await decorationStatusPromise;
// Handling the results console.log('Food Status:', foodStatus); console.log('Decoration Status:', decorationStatus);
if (foodStatus === 'Food has arrived!' && decorationStatus === 'Decorations are ready!') { console.log('Everything is ready for the wedding!'); } else { console.log('There are delays. Please wait for further updates.'); } } catch (error) { console.error('An error occurred:', error.message); }
console.log('Coordination process completed.'); }
// Run the main function coordinateWedding(); ```
### Explanation
1. **Axios Package:** We use Axios to simulate API calls. In a real-world scenario, you would use Axios to fetch data from actual endpoints. 2. **Simulated Delays:** We use `setTimeout` to simulate network delays for fetching food and decoration statuses. 3. **Asynchronous Functions:** `fetchFoodArrivalStatus` and `fetchDecorationSetupStatus` are declared as `async` functions, allowing the use of `await` to pause their execution until the simulated API calls complete. 4. **Concurrent Execution:** The `coordinateWedding` function fetches the statuses concurrently using `Promise.all`. 5. **Error Handling:** The `try...catch` block handles any potential errors that might occur during the asynchronous operations.
### Step 3: Run the Code
1. **Open the Project in Visual Studio Code:** - Open Visual Studio Code. - Click `File` > `Open Folder` and select the `wedding-planner` directory.
2. **Run the Script:** - Open the terminal in Visual Studio Code by clicking `Terminal` > `New Terminal`. - Run the script using Node.js: ```bash node index.js ```
You should see the output of the coordination process in the terminal, displaying the statuses of food and decoration arrivals and indicating whether everything is ready for the wedding or if there are delays.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
) instead.