자바스크립트

프로마이스 연습

봄산 2019. 1. 18. 09:30
doSomething()
.then( result => doSomethingElse(result) )
.then(newResult => doThirdThing(newResult))
.then(finalResult => { console.log(`Got the final result: ${finalResult}`); })
.catch(failureCallback);


function doSomething(){
return new Promise(function(resolve,reject){

setTimeout(function() {
console.log('doSomething');

resolve('result1');
}, 1000);
})

}

function doSomethingElse(){

return new Promise(function(resolve,reject){

setTimeout(function() {
console.log('doSomethingElse');

resolve('result2');
}, 1000);
})


}

function doThirdThing(){

return new Promise(function(resolve,reject){

setTimeout(function() {
console.log('doThirdThing');
resolve('result3');
}, 1000);
})


}


function failureCallback(){

console.log('call last call');

}


결과

doSomething

doSomethingElse

doThirdThing

Got the final result: result3