Skip to content
← Back to rules

promise/no-callback-in-promise 정확성

작동 방식

Promise.prototype.then() 또는 Promise.prototype.catch() 내부에서 콜백 함수(cb())를 호출하는 것을 금지합니다.

왜 나쁜가요?

then() 또는 catch() 메서드 내부에서 콜백을 직접 호출하면 예기치 않은 동작, 예를 들어 콜백이 여러 번 호출되는 경우가 발생할 수 있습니다. 또한 이렇게 콜백과 프로미스 패러다임을 혼합하면 코드가 복잡해지고 유지보수하기 어려워집니다.

예시

이 규칙에 위반되는 잘못된 코드 예시:

js
function callback(err, data) {
  console.log("콜백이 다음 값으로 호출됨:", err, data);
  throw new Error("내 오류");
}

Promise.resolve()
  .then(() => callback(null, "data"))
  .catch((err) => callback(err.message, null));

이 규칙에 맞는 올바른 코드 예시:

js
Promise.resolve()
  .then((data) => {
    console.log(data);
  })
  .catch((err) => {
    console.error(err);
  });

구성

이 규칙은 다음 속성을 가진 구성 객체를 수용합니다.

callbacks

type: string[]

기본값: ["callback", "cb", "done", "next"]

Promise의 thencatch 메서드 내에서 확인할 콜백 함수 이름 목록입니다.

exceptions

type: string[]

기본값: []

Promise의 thencatch 메서드 내에서 허용할 콜백 함수 이름 목록입니다.

timeoutsErr

type: boolean

기본값: false

setTimeout과 같은 타임아웃 함수 내의 콜백이 오류로 처리될지 여부를 지정하는 부울 값입니다.

사용 방법

이 규칙을 구성 파일이나 명령줄 인터페이스에서 활성화하려면 다음을 사용할 수 있습니다:

json
{
  "plugins": ["promise"],
  "rules": {
    "promise/no-callback-in-promise": "error"
  }
}
bash
oxlint --deny promise/no-callback-in-promise --promise-plugin

참고 자료