관련 포스팅
Promise: 간편하게 비동기 작업을 처리해주는 자바스크립트 객체
코드A
const getData = new Promise((resolve, reject)=>{
resolve("🍎")
reject(new Error("Promise 객체 시험 강제 중단"))
})
state:
Pending -> Fullfilled
혹은
Pending -> Rejected
Promise가 생성되자마자 executor함수가 바로 실행된다.
//case1
const getData = new Promise((resolve, reject)=>{
setTimeout(()=>resolve("🍎"), 3000)
})
getData.then((res)=>console.log(res))
//case2
const getData = (ms)=>{
return new Promise(resolve => setTimeout(resolve, ms))
}
getData(3000).then(()=>console.log("3초 지연 후 실행"))
const getData = new Promise(res=>res(5))
getData
.then((data)=>{
return data *= 10 //50
})
.then((data)=.{
return data *= 10 //500
})
.then((data)=.{
return data *= 10 //5000
})
doPromise
.then((받아온값)=>{
return console.log(`당신은${받아온값}을 받아오는 데 성공하였습니다.`)
}).catch(error=>{
return console.log(`오류가 발생하였습니다: ${error}`)
}).finally(()=>{
console.log("프로미스 객체 시험이 모두 완료되었습니다.")})
References
https://ko.javascript.info/promise-basics
https://www.youtube.com/watch?v=JB_yU6Oe2eE
https://joshua1988.github.io/web-development/javascript/promise-for-beginners/