Skip to content
Advertisement

Eslint: how to handle no-use-before-define for dependent functions?

I have 2 dependent functions that call each others, so one or the other has to be declared first which fires an eslint no-use-before-define error. I know I can disable the rule but is there any better way do do this?

Simplified example:

const a = number => {
  if (number === 0) {
    return b(number);
  }
  
  c(number);
}

const b = number => a(number + 1);

a(0);

I can’t merge a and b as they both need to be called separately somewhere else in the code.

Advertisement

Answer

You may use callback or higher order functions. Hope that will help. Plz try this code and leme know if it works fine with your linting rules.

const a = (number, call) => {
  if (number === 0) {
    return call(number);
  }
  c(number);
}

const b = number => a(number + 1, b);

const c = number => console.log(1);

a(0, b);
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement