4

I am new to promise and I am wondering which is the best practice with native Promise (NodeJs).

I put some code below to better understand the question:

Code A

function foo(condition) {
return new Promise((resolve, reject) => {
    if(condition){
        resolve('Promise result!');
    } else {
        reject('Promise rejected!');
    }
});
} 

Code B

function foo(condition) {
return new Promise((resolve, reject) => {
    if(condition){
        return resolve('Promise result!');
    } else {
        return reject('Promise rejected!');
    }
});
}

In solution B, I add the return statement.

Is it useful?

Which are the difference between Code A and Code B? If any?

enricop89
  • 352
  • 2
  • 13

1 Answers1

3

Short answer: It does not matter if you return or not.

The spec tells us that the return value of the executor is not used. It is only checked if the call returned an abrupt completion. (throwing an Error causes an abrupt completion )If a abrupt completion happend the promise is rejected.

Till Arnold
  • 561
  • 2
  • 11
  • 5
    You are right saying that the return value is not used, though, it is sometimes useful to return after a reject (if you do not want the rest of the method to be executed). – Antoine Boisier-Michaud Jun 14 '17 at 19:06