2

Is it more correct to use return when calling resolve / reject for an ES6 promise? Does this help with the call stack?

return new Promise((resolve, reject) => {           
this._mariadb.query(...,
    (err, rows) => {             
            if (err) {return reject(err);}                
            if (rows.info.numRows > 0) {return resolve(true);}        
            return resolve(false);                
        });            
    });

3 Answers3

3

The return value isn't used but it can be a quick way to exit the function early. That's all. The code you have above could be written to function the exact same way without return

if (err)
  reject(err);
else if (rows.info.numRows > 0)
  resolve(true);
else
  resolve(false);

Or even better

if (err)
  reject(err);
else
  resolve(rows.info.numRows > 0);
Mulan
  • 129,518
  • 31
  • 228
  • 259
2

return reject(err) is just a more concise (but also more confusing) way to say reject(err); return. The argument of the Promise ("executor") isn't supposed to return anything meaningful, so no, it's doesn't matter if you use return or not.

georg
  • 211,518
  • 52
  • 313
  • 390
-1

No, you shouldn't because once promise is resolved/rejected it is never change state

maxpolski
  • 11
  • 1
  • 3