Search code examples
sonarqubeazure-bot-service

“await” should not be used redundantly for bot framework


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 ?


Solution

  • 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.