Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 885
adesurirey Avatar asked Sep 03 '25 06:09

adesurirey


1 Answers

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);
like image 177
Azeem Aslam Avatar answered Sep 04 '25 20:09

Azeem Aslam