async await 장점

dev.dave·2023년 7월 24일

Javascript

목록 보기
16/167

프로미스 체이닝방식
function test() {
fetch("https://api.testdomain.com")
.then(response => {
// do something
})
.catch(error => {
//do something
});
}


에이싱크 어웨잇
async function test() {
try {
const response = await fetch("...url...")
// do something
} catch (error) {
// handle error
}
}

==========================

또다른예)

프로미스 체이닝방식
function test() {
return dosomethingAsync()
.then(hello => doAsync2(hello))
.then(world => doAsync3(world))
.then(foo => doAsync4(foo))
.then(bar => doAsync5(bar))
}


에이싱크 어웨잇
async function test() {
const hello = await dosome1();
const world = await dosome2(hello);
const foo = await dosome3(world);
const bar = await dosome4(foo);
await dosome5(bar);
}

============================

프로미스 체이닝 방식은
then하고 그 생명주기가 끝이나는데,

에이싱크방식은
계속 어느곳에서도 다시쓰이는게 가능하다.
쓸일이 없는데도,,,ㅜㅜ

profile
🔥개인 메모 / 다른블로그 자료 참조 / 다른블로그 자료 퍼옴 (출처표기) /여기저기서 공부 했던 내용 개인메모 & 참고 / 개인 기록 용도 블로그 입니다.🔥

0개의 댓글