-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpromise.js
More file actions
54 lines (40 loc) 路 1.45 KB
/
Copy pathpromise.js
File metadata and controls
54 lines (40 loc) 路 1.45 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
//: Promise In Javascript
//* In Javascript, a promise is a good way to handle asynchronous operations. It is used to find out if the asynchronous operation is successfully completed or not */
//! A promise may have one of three states.
/* Pending
Fulfilled
Rejected
*/
//* A promise starts in a pending state. That means the process is not complete. If the operation is successful, the process ends in a fulfilled state. And, if an error occurs, the process ends in a rejected state*/
//! create a Promise
//* To create a promise object, we use the Promise() constuctor
let promis = new Promise(function (resolve, reject) {
// do something
});
//? The Promise() constuctor takes a function as an argument. The function also accepts two functions resolve() and reject().
// If the promise returns successfully, the resolve() function is called. And, if an error occurs, the reject() function is called
var promise = new Promise(function (resolve, reject) {
var x = 1;
var y = 1;
if (x === y) {
console.log("Success, You are a Welcome");
resolve();
} else {
console.log("Some Error has occurred");
reject();
}
});
promise
.then(function () {
console.log("Success");
})
.catch(function () {
console.log("Error");
});
/**
//* Benefits Of Promise
1. Improve Code Readabillity
2. Better handling of asynchronous operation
3. Better flow of control definition in asynchronous logic
4. Better Error handling
*/