"Refactor this redundant 'await' on a non-promise." is not correct

In our company we’re using SonarCloud which reports the following error:

Refactor this redundant ‘await’ on a non-promise.

This does not work with Mongoose where they add a then method inside the prototype of function. This is already enough to create a Promise. My guess is that SonarCloud checks if the code is an instanceof Promise which is not the case with Mongoose. It’s a function named Query and when you await it will call the prototype.then method.

Here is an example of how Mongoose does it:

function Query() {}
Query.prototype.then = function(resolve, reject) {
  resolve(true);
};

async function main() {
  const query = new Query();
  console.log(query instanceof Promise); // false 

  const result = await query;
  console.log(); // true
}

main();

Can this be fixed in SonarCloud? Thanks.

Hi @kkoomen,

Rule S4123 implementation checks if given object has a property called then so it should cover the case of Mongoose.

Can you share a reproducer of the false positive you encountered?

It’s not Mongoose specifically, there are more libraries who do this. The async keyword wraps the expression after it into a promise and resolves it. A valid promise can be by using new Promise() or by using an object that contains then in the prototype.

Can you share a reproducer of the false positive you encountered?

See the code example I added in my description. It’s a perfect reproducible scenario.

I’m experiencing this issue as well using React, Redux (Redux-Thunk) on actions that I am dispatching that clearly return promises

Code: await dispatch(getThings(someId, value));