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?
>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.