-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPromise.js
More file actions
33 lines (26 loc) · 1.02 KB
/
Promise.js
File metadata and controls
33 lines (26 loc) · 1.02 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
// A Promise is a placeholder for a future value, allowing you to perform asynchronous tasks
// For example, network requests take time, and without promises, they would block your program
// Promises have 3 states: fulfilled, rejected, and pending
// How to create a Promise
// Some functions automatically return a promise (e.g., fetch(), async functions)
// You can manually create a promise using the Promise constructor function
const p = new Promise((resolve, reject) => {
const count = 5
if (count < 10) {
resolve('Resolved') // Resolve the promise if the condition is met
} else {
reject('Rejected') // Reject the promise if the condition is not met
}
})
// When the future value arrives, you handle it with .then(), .catch(), and .finally()
p.then(() => {
console.log(p)
console.log('Fulfilled!')
}).catch(() => {
console.log(p)
console.log('Rejected!')
}).finally(() => {
console.log(p)
console.log('Finally')
})
// You can also resolve and reject promises with a value