-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathes6_9_promise.js
53 lines (45 loc) · 1013 Bytes
/
es6_9_promise.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
* ES6/ES2015
* Promise
* A promise object represents an eventual completion or failure for an asynchronous operation.
* @author Mahmud Ahsan
* {@link https://github.com/mahmudahsan/javascript}
*/
const getData = () => {
return new Promise((resolve, reject) => {
setTimeout(()=>{
resolve('data received');
}, 1000);
// reject example
setTimeout(()=>{
reject('network disconnected');
}, 500);
});
};
const dataFromServer = getData();
dataFromServer.then(
(value)=>{
console.log(value);
},
(error)=>{
console.log(error);
}
);
/*
//catch to redetect reject
dataFromServer.then(
(value)=>{
console.log(value);
})
.catch((error)=>{
console.log(error);
}
);
*/
/**
* Promise has 3 states
* pending | fulfilled | rejected
*/
/**
* When a pending promise either fulfilled or rejected and if a corresponding handler is attached by 'then' method, the handler will be called.
*/