Promise || Async/Await

callback방식

1
2
3
4
5
6
7
8
9
10
11
function asyncFunction(callback) {
setTimeout(function() {
if (callback) callback(1);
}, 1000);
}

function caller() {
asyncFunction(function(returnVal) {
console.log('after callback()');
});
}

promise

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function asyncFunction() {
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve(1);
}, 1000);
});
}
function caller() {
asyncFunction()
.then(function resolved(resolveVal) {
console.log('after callback()');
})
.catch(function rejected() { });
}

Async/Await / ES7

1
2
3
4
5
6
7
8
async function caller() {
try {
const resolveVal = await asyncFunction();
console.log('after callback()');
} catch (rejected) {
console.log('after reject(throw)');
}
};

Async/Await은 Await을 사용하기 위해 함수를 async로 선언해야 한다. TypeScript는 1.7버전 이상이 있으면 사용 가능하다.

마지막으로 async 함수는 Promise를 리턴한다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
async function returnPromise(wantToThrow) {
if (wantToThrow) {
throw false;
}
return true;
}

async function awaiter() {
const mustBeTrue = await returnPromise();
console.log(mustBeTrue) // true

try {
await returnPromise(true);
} catch(ex) {
console.log(ex); // false
}
}
코드를 넘어