Promise executor functions should not be async

P

문제상황

function ready() {
  return new Promise<void>((resolve) => {
    console.log('game ready');
    resolve();
  });
}
function end() {
  return new Promise<void>(async (resolve) => {
    await matrixReset();
    resolve();
  });
}
function matrixReset() {
  return new Promise<void>((resolve) => {
    setTimeout(() => {
      console.log('matrix is reset!!');
      resolve();
    }, 2000);
  });
}
async function over() {
  await end();
  await ready();
}

end함수의 Promise에서 Promise executor functions should not be async. 라는 에러 메세지가 뜬다.

문제 해결 과정

참고 링크

  1. https://velog.io/@hanbinleejoy/Promise-%EC%83%9D%EC%84%B1%EC%9E%90-%EB%82%B4-async-await-%EB%84%A3%EA%B8%B0
  2. https://stackoverflow.com/questions/43083696/cant-throw-error-from-within-an-async-promise-executor-function

promise executor함수에 async를 쓰면 안된단다.

해결 방법

function ready() {
  return new Promise<void>((resolve) => {
    console.log('game ready');
    resolve();
  });
}
function matrixReset() {
  return new Promise<void>((resolve) => {
    setTimeout(() => {
      console.log('matrix is reset!!');
      resolve();
    }, 2000);
  });
}
function end() {
  return new Promise<void>((resolve) => {
    matrixReset().then(() => resolve());
  });
}
async function over() {
  await end();
  await ready();
}

왜 쓰면 안되는지는 잘 모르겠지만, 일단 이렇게 고쳤다.

Add Comment