-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromise.js
55 lines (45 loc) · 1.05 KB
/
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
54
55
/**
* basic Promise
*/
const promise = new Promise( (resolve,reject) => {
resolve(42);
});
promise.then(function(value){
console.log(value);
}).catch(function(error){
console.error(error);
});
/**
* asyncFunction with Promise
*/
const asyncFunction = () => {
return new Promise((resolve, reject) =>{
setTimeout( () => {
resolve('Async Hello world');
}, 16);
});
}
//use catch
asyncFunction()
.then((value) => {
console.log(value); // => 'Async Hello world'
}).catch( (error) => {
console.error(error);
});
//promise.then(onFulfilled, onRejected)
asyncFunction().then(
(value) => {
console.log(value); // => 'Async Hello world'
},
(error) => {
console.error(error);
}
);
/**
Fulfilled
resolve(成功)した時。このとき onFulfilled が呼ばれる
Rejected
reject(失敗)した時。このとき onRejected が呼ばれる
Pending
FulfilledまたはRejectedではない時。つまりpromiseオブジェクトが作成された初期状態等が該当する
*/