I like the async await syntax, and I use it a lot with mongoose.
So in my project there is plenty of :
const user = await User.findOne({
_id: req.params.id
})
Which works just as expected. However, in sonarqube, I have these errors :
Refactor this redundant 'await' on a non-promise.
And the sonarqube rule i :
It is possible to use await on values which are not Promises, but it's useless and misleading. The point of await is to pause execution until the Promise's asynchronous code has run to completion. With anything other than a Promise, there's nothing to wait for.
This rule raises an issue when an awaited value is guaranteed not to be a Promise.
Noncompliant Code Example
let x = 42;
await x; // Noncompliant
Compliant Solution
let x = new Promise(resolve => resolve(42));
await x;
let y = p ? 42 : new Promise(resolve => resolve(42));
await y;
I am using mongo 4.0 and mongoose 5.3.1
Since I can use the .then
, .catch
syntax I thought that I was dealing with promise so how could I fix that ?
所有评论(0)