본문 바로가기
IT/Python Quiz

[javascript] 비동기 처리 퀴즈

by Echinacea 2025. 2. 16.
반응형

 

퀴즈 문제

 

1. setTimeout()의 실행 순서

console.log("A");
setTimeout(() => {
    console.log("B");
}, 2000);
console.log("C");

콘솔 출력 결과는?

  • a) A → B → C
  • b) A → C → B
  • c) B → A → C
  • d) 실행되지 않음

 

2. Promise의 실행 흐름

console.log("Start");
const promise = new Promise((resolve) => {
    console.log("Inside Promise");
    resolve("Done");
});
promise.then((result) => console.log(result));
console.log("End");

콘솔 출력 결과는?

  • a) Start → Inside Promise → End → Done
  • b) Inside Promise → Start → End → Done
  • c) Start → End → Inside Promise → Done
  • d) Start → Inside Promise → Done → End

 

3. async/await의 실행 흐름

async function test() {
    console.log("1");
    await new Promise((resolve) => setTimeout(resolve, 1000));
    console.log("2");
}
console.log("3");
test();
console.log("4");

콘솔 출력 결과는?

  • a) 1 → 3 → 2 → 4
  • b) 3 → 1 → 4 → 2
  • c) 3 → 1 → 2 → 4
  • d) 1 → 2 → 3 → 4

 

 

4. catch()를 활용한 예외 처리

const promise = new Promise((_, reject) => reject("Error"));
promise.catch((err) => console.log("Caught:", err));

출력 결과는?

  • a) Caught: Error
  • b) undefined
  • c) 오류 발생
  • d) 아무것도 출력되지 않음

 

5. finally() 블록의 실행 여부

const promise = new Promise((resolve, reject) => {
    reject("Fail");
}).catch((err) => {
    console.log("Caught:", err);
}).finally(() => {
    console.log("Finally 실행됨");
});

출력 결과는?

  • a) Caught: Fail → Finally 실행됨
  • b) Finally 실행됨 → Caught: Fail
  • c) 아무것도 출력되지 않음
  • d) 오류 발생

 

6. Promise.all()의 동작 방식

const p1 = Promise.resolve("첫 번째 완료");
const p2 = new Promise((resolve) => setTimeout(() => resolve("두 번째 완료"), 1000));
Promise.all([p1, p2]).then(console.log);

출력 결과는?

  • a) ["첫 번째 완료", "두 번째 완료"] (즉시 출력)
  • b) ["첫 번째 완료", "두 번째 완료"] (1초 후 출력)
  • c) "첫 번째 완료" → "두 번째 완료"
  • d) 오류 발생

 

7. async 함수의 반환값

async function fetchData() {
    return "데이터 반환";
}
fetchData().then(console.log);

출력 결과는?

  • a) "데이터 반환"
  • b) Promise { "데이터 반환" }
  • c) undefined
  • d) 오류 발생

 

8. await의 블로킹 여부

async function test() {
    console.log("A");
    await new Promise((resolve) => setTimeout(resolve, 1000));
    console.log("B");
}
test();
console.log("C");

출력 결과는?

  • a) A → B → C
  • b) A → C → B
  • c) C → A → B
  • d) 오류 발생

 

9. Promise.race()의 동작 방식

const p1 = new Promise((resolve) => setTimeout(() => resolve("첫 번째"), 1000));
const p2 = new Promise((resolve) => setTimeout(() => resolve("두 번째"), 500));
Promise.race([p1, p2]).then(console.log);

출력 결과는?

  • a) "첫 번째" (1초 후 출력)
  • b) "두 번째" (0.5초 후 출력)
  • c) "첫 번째", "두 번째"
  • d) 오류 발생

 

10. Promise.reject()가 처리되지 않은 경우

const promise = new Promise((_, reject) => reject("에러 발생"));

출력 결과는?

  • a) 아무 출력 없음
  • b) UnhandledPromiseRejectionWarning
  • c) "에러 발생"
  • d) 오류 발생

 

 

 

 

 

 

 

 

정답 및 해설

  1. 정답: b) setTimeout()이 비동기 실행되므로 A → C → B 순서로 실행됩니다.
  2. 정답: a) Promise 내부 코드는 즉시 실행되므로 Inside Promise가 먼저 출력됩니다.
  3. 정답: b) 31이 먼저 실행되고, await로 인해 2가 나중에 실행됩니다.
  4. 정답: a) catch()reject()에서 전달된 값을 받아 Caught: Error를 출력합니다.
  5. 정답: a) finally() 블록은 catch()가 실행된 후에도 실행됩니다.
  6. 정답: b) Promise.all()은 모든 Promise가 완료될 때까지 기다린 후 결과를 반환합니다.
  7. 정답: b) async 함수는 항상 Promise를 반환하므로 Promise { "데이터 반환" }가 출력됩니다.
  8. 정답: b) await 이후 코드는 블로킹되지 않고, console.log("C")가 먼저 실행됩니다.
  9. 정답: b) Promise.race()는 가장 먼저 완료된 Promise의 값을 반환합니다.
  10. 정답: b) Promise.reject()가 처리되지 않으면 UnhandledPromiseRejectionWarning이 발생합니다.

 

반응형

댓글