Intermediate JavaScript Exercise: Asynchronous Programming in E-Commerce Scenario #242
akash-coded
started this conversation in
Tasks
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Intermediate JavaScript Exercise: Asynchronous Programming in E-Commerce Scenario
Objective
Develop an understanding of asynchronous programming in JavaScript through a series of tasks simulating e-commerce operations, such as fetching product details, checking inventory, and processing orders. Focus on using Promises, async-await, and callbacks without relying on server-side frameworks.
Environment Setup
Since this exercise avoids server-side complexities like Express, we'll simulate asynchronous operations using the
fetchAPI for network requests andsetTimeoutfor mimicking delays in local operations.Scenario Overview
You are tasked with building several functions to handle operations for an online store. These include fetching product details from a mock API, checking inventory levels, and processing customer orders.
Task 1: Fetching Product Details with Promises
fetchAPI to retrieve product details from a mock API endpoint.fetchProductDetails(productId)that takes a product ID and returns a promise with the product details.https://fakestoreapi.com/products/as your base URL. Append the product ID to fetch details for a specific product..then()and.catch()to handle the promise returned by thefetchAPI.Task 2: Checking Inventory with Callbacks
checkInventory(productId, callback)that simulates checking inventory for a given product ID. UsesetTimeoutto mimic a delay.callback(error, result). If the product is in stock, returntrueas the result; otherwise,false.Task 3: Processing Orders with Async/Await
processOrder(productId)that awaits both the product details and the inventory check before proceeding.Task 4: Handling Multiple Orders Concurrently
processMultipleOrders(productIds)that takes an array of product IDs and processes each order concurrently.Promise.allto await all orders. Ensure to handle both successful and failed order processing.Task 5: Implementing Retry Logic for Network Requests
fetchProductDetailsfunction to handle transient network errors.fetchProductDetailsto retry the fetch request up to 3 times in case of failure before giving up.Task 6: Creating a Robust Order Processing Function
processOrderto check for invalid product IDs, out-of-stock items, and network issues, providing clear feedback for each scenario.Best Practices and Cautions
Promise.allwith care, as it rejects immediately upon any of the promises rejecting.Deliverables
Additional Learning Activities
fetchProductDetailsto avoid hitting the API too frequently.AbortControlleror other mechanisms.This exercise aims to solidify intermediate-level asynchronous programming concepts in a practical, scenario-based manner, focusing on real-world application in the e-commerce domain.
Beta Was this translation helpful? Give feedback.
All reactions