Promise
const a = () => {
return new Promise((resolve)=>{
setTimeout(()=>{
console.log(1);
resolve();
},1000)
})
}
const b = () => {
return new Promise((resolve)=>{
setTimeout(()=>{
console.log(2);
resolve();
},1000)
})
}
const c = () => {
return new Promise((resolve)=>{
setTimeout(()=>{
console.log(3);
resolve();
},1000)
})
}
const d = () => console.log(4);
a().then(()=>{
return b();
}).then(()=>{
return c();
}).then(()=>{
return d();
})
a()
.then(()=> b())
.then(()=> c())
.then(()=> d())
a()
.then(b)
.then(c)
.then(d)
- 비동기로 동작하는 코드를 promise 생성자를 통해서 정리를 해주면 then 이라는 메서드를 통해서 순서대로 작성할수 있으며 , 훨신 더 간결한 패턴으로 비동기 코드에서의 다음순서보장을 만들수 있다.