기본 형태
//resolve 로 정상일 경우
new Promise(function(resolve, reject) {
resolve('hello'); //goto then()
}).then(function(result){
console.log("success : "+ result); // success : hello
}).catch(function(result){
console.log("failed : "+ result);
});
//reject 로 실패일 경우
new Promise(function(resolve, reject) {
reject('hello'); //goto catch()
}).then(function(result){
console.log("success : "+ result);
}).catch(function(result){
console.log("failed : "+ result); // failed : hello
});
함수형으로 분리하여 사용
//함수형으로 분리할 경우
function hello() {
return new Promise(function(resolve, reject) {
resolve('hello'); //goto then()
});
}
hello().then(function(result) {
console.log(result); // hello
throw new Error("then() Error"); //goto catch()
}).catch(function(err) {
console.log(err); // then() Error
});