Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
141 lines (112 loc) · 3.3 KB
/
Copy pathmain.js
File metadata and controls
141 lines (112 loc) · 3.3 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
console.log("Before time-consuming task (Synchronous)");
const start = new Date().getTime();
while (new Date().getTime() < start + 5000); // 5 seconds delay
console.log("After time-consuming task (Synchronous)");
console.log("Before time-consuming task (Asynchronous)");
setTimeout(function () {
console.log("Time-consuming task complete (Asynchronous)");
}, 5000); // 5 seconds delay
console.log("After time-consuming task (Asynchronous)");
setTimeout(() => {
console.log("This will run after 2 seconds");
}, 2000);
let promise = new Promise((resolve, reject) => {
let a = 1 + 1;
if (a == 2) {
resolve("Success");
} else {
reject("Failed");
}
});
promise
.then((message) => {
console.log("This is in the then " + message);
})
.catch((message) => {
console.log("This is the catch " + message);
});
/**
* Fetches data from the specified URL using the Fetch API.
* @param {string} url - The URL to fetch data from.
* @returns {Promise} A Promise that resolves with the parsed JSON data if the response is successful, or rejects with an error.
*/
function fetchData(url) {
return new Promise((resolve, reject) => {
fetch(url)
.then((response) => {
if (response.ok) {
resolve(response.json());
} else {
reject(new Error("Failed to fetch data"));
}
})
.catch((error) => reject(error));
});
}
// Usage
fetchData("https://api.github.com/users/openai")
.then((data) => {
console.log(data);
})
.catch((error) => {
console.error("Error:", error);
});
async function fetchUser(username) {
try {
const response = await fetch(`https://api.github.com/users/${username}`);
if (response.ok) {
const user = await response.json();
console.log(user);
} else {
throw new Error("Failed to fetch user");
}
} catch (error) {
console.error("Error:", error);
}
}
fetchUser("mchamoudadev");
function checkInventory(order) {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (order.quantity <= 100) {
resolve(order);
} else {
reject("Not enough inventory");
}
}, 1000);
});
}
function processPayment(order) {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (order.paymentMethod === "Credit Card") {
resolve(order);
} else {
reject("Unsupported payment method");
}
}, 1000);
});
}
function shipOrder(order) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(`Order shipped: ${order.item}`);
}, 1000);
});
}
let order = {
item: "Drone",
quantity: 1,
paymentMethod: "Credit Card",
};
async function processOrder(order) {
try {
await checkInventory(order);
await processPayment(order);
const shippingStatus = await shipOrder(order);
console.log(shippingStatus);
} catch (error) {
console.error(error);
}
}
processOrder(order);