0

We have developed a chatbot using Azure bot framework. As part of our CI-CD pipeline, we use Sonar Qube to do static code analysis.

Sonar shows multiple instances of code smells as “Redundant use of await on a return value”. The recommendation from Sonar is not to use await as the async method is expected to use a promise.

However, this approach is taken from the BOT Framework samples provided by Microsoft (https://github.com/microsoft/BotBuilder-Samples/blob/main/samples/typescript_nodejs/13.core-bot/src/dialogs/bookingDialog.ts)

Can you please confirm if Microsoft recommendation has changed or this seems to be false positive alert from SonarQube ?

  • What do you mean when you say "The recommendation from Sonar is not to use await as the async method is expected to use a promise"? It sounds like you're saying "await" and "a promise" are two different ways of doing things, but that doesn't make sense because they're the same. Promises are what you await. You await promises. – Kyle Delaney May 21 '21 at 19:21
  • @KyleDelaney: SonarQube shows this as a code smell "Redundant use of `await` on a return value" – Vishal Goyal May 24 '21 at 04:50
  • Yes, I saw that in your question already. Do you understand what I'm asking? – Kyle Delaney May 24 '21 at 21:01

1 Answers1

1

First of all, this Sonar rule was added about 2 years ago in this Pull Request with this example

I then found those SO articles answering similar questions: article 1, article 2 but it was still unclear to me so I kept on looking.

Finally I reviewed this documentation and found the answer I was looking for in the last example provided.

In the above example, notice there is no await statement after the return keyword, although that would be valid too: The return value of an async function is implicitly wrapped in Promise.resolve - if it's not already a promise itself (as in this example).

Note: The implicit wrapping of return values in Promise.resolve does not imply that return await promiseValue is functionally equivalent to return promiseValue.

I tried the error handling with and without the await on my project and ended up removing the await triggering the warning. So far I haven't seen any difference. I have also noticed that if you wrap the same code inside a try / catch, the Sonar warning isn't raised anymore.

From now on, I will follow Sonar's advice but will update this thread if I encounter an issue.

Dharman
  • 30,962
  • 25
  • 85
  • 135