02_promise.js
957 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"use strict";
function asyncTask1(cb) {
console.log("task1 start");
setTimeout(function() {
cb("task1");
}, 2000);
}
function asyncTask2(cb) {
console.log("task2 start");
setTimeout(function() {
cb("task2");
}, 1000);
}
asyncTask2(function(ret) {
console.log(ret + " done");
asyncTask1(function(ret2) {
console.log(ret2 + "done");
// 추가적인 callback이 더 생긴다면???
});
});
function promiseTask1() {
console.log("task1 start");
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve('task1');
}, 2000);
});
}
function promiseTask2() {
console.log("task2 start");
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve('task2');
}, 1000);
});
}
promiseTask2()
.then(function(ret) {
console.log(ret + " done");
}).then(function() {
return promiseTask1();
})
.then(function(ret) {
console.log(ret + " done");
});