akxltmzk
8/2/2019 - 1:59 AM

promise(비동기 처리)

function DelayP(sec){
  return new Promise((resolve,reject)=>{
   setTimeout(()=>{
    resolve('resolve 출력') 
   },sec * 1000) 
  })
}

DelayP(1).then((result)=>{
  console.log(1, result)
  return DelayP(1)
}).then((result)=>{
  console.log(2, result)
  return DelayP(1)
}).then((result)=>{
  console.log(3, result)
  return 'wow'
}).then((result)=>{
  console.log(result)
})
function DelayP(sec){
  return new Promise((resolve,reject)=>{
    setTimeout(()=>{
      reject('error!')
    }, sec*1000)
  })
}


DelayP(3)
  .then(
    ()=>{ console.log('done') } , 
    e =>{console.log('1nd catch in Then'), e , throw new Error("throw in Then")}
  )
  .catch(e=>{
    console.log('2st catch', e)
  })