-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprob9.js
31 lines (25 loc) · 1.14 KB
/
prob9.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// The Asynchronous Shopper: Imagine you are building an online shopping application. Write an asynchronous function called placeOrder that simulates placing an order and returns a promise. The promise should resolve with an order confirmation message after a random delay.
async function placeOrder(orderDetails) {
return new Promise((resolve, reject) => {
if (!orderDetails || !orderDetails.item) {
reject("Invalid order details provided");
return;
}
const delay = Math.floor(Math.random() * 2000) + 1000; // Random delay between 1-3 seconds
setTimeout(() => {
resolve(`Order confirmed: ${orderDetails.item} will be delivered soon.`);
}, delay);
});
}
// Example usage:
async function main() {
try {
const orderDetails = { item: "Laptop", quantity: 1 };
console.log("Placing order...");
const confirmation = await placeOrder(orderDetails);
console.log(confirmation); // Output: "Order confirmed: Laptop will be delivered soon."
} catch (error) {
console.error(error); // Handle errors like invalid order details
}
}
main();