-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathPromise.js
More file actions
30 lines (26 loc) · 841 Bytes
/
Copy pathPromise.js
File metadata and controls
30 lines (26 loc) · 841 Bytes
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
// Creating a new Promise
const myPromise = new Promise((resolve, reject) => {
const success = true; // Simulate success or failure
setTimeout(() => {
if (success) {
resolve("Promise resolved successfully!");
} else {
reject("Promise rejected!");
}
}, 2000);
});
// Using the Promise
myPromise
.then((message) => {
console.log(message); // "Promise resolved successfully!" after 2 seconds
})
.catch((error) => {
console.error(error); // If success was false, this would log "Promise rejected!"
})
.finally(() => {
console.log("Promise settled (either resolved or rejected).");
});
// Output:
// (after 2 seconds)
// Promise resolved successfully!
// Promise settled (either resolved or rejected).