async/await(2)
-
Javascript Async / Await
async function myFunc() { try { const res1 = await fetch("URL 입력"); const req1 = await res1.json(); console.log("첫번째 Request 완료", req1); const res2 = await fetch("URL 입력"); const req2 = await res2.json(); console.log("두번째 Request 완료", req2); } catch (err) { console.log(err); } finally { console.log("done"); } } myFunc(); - 좋은 가독성 promise, callback 보다 훨씬 보기 편함 - await 는 async 내부 함수에만 사용 가능
2023.05.16 -
Promise, async / await [node.js 교과서]
본 게시글은 node.js 교과서 강의를 듣고 정리한 글입니다. - 콜백 헬이라고 불리는 지저분한 자바스크립트 코드의 해결책이다. - Promise : 내용이 실행은 되었지만 결과를 아직 반환하지 않은 객체 - then 을 붙이면 결과를 반환한다. - 실행이 완료되지 않았으면 완료된 후에 then 의 내부함수가 실행된다. const condition = true; const promise = new Promise((resulve, reject) ⇒ { if(condition) { resolve(’성공’); } else { reject(’실패’); } }); promise.then((message) ⇒ { console.log(message); }) .catch((err) ⇒ { console.log(er..
2023.04.08