Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

eslint no-useless-return in Promise

I just realised that eslint’s no-useless-returns is yelling at me for writing the following…

const success = await new Promise((resolve, reject) => {
      let timer = setTimeout(() => {
        reject();
        return; <----
      }, 5000);
      pubs.on('ok', () => {
        console.log('okey!');
        clearTimeout(timer);
        resolve(true);
      });
    });

Now I understand what the rule is supposed to tell me, however afaik code execution would continue even after the promise rejected, right? So why is this considered an useless return?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

That return is not related callback function of the Promise:

(resolve, reject) => {
   // ... 
}

It belongs belongs to the arrow function, passed to the setTimeout

() => {
  reject();
  return;
}

and for that reason, only exits that function, and being the last statement in that function and not returning anything it is useless.

Even if setTimeout would do something with the value returned from that function, it would still be useless because return; is the only return statement, returns undefined and a function without a return would also return undefined.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading