Skip to content

BSL154 — Lines of code after the asynchronous method call

Summary

Lines of code after the asynchronous method call

Identifiers

Field Value
Rule code BSL154
Compatible alias CodeAfterAsyncCall
Severity WARNING
Enabled by default Yes
Implemented Yes
Tags async, correctness

Behavior

  • The public identifier BSL154 and alias CodeAfterAsyncCall are stable.
  • The rule reports the cases documented on this page.
  • Suppressions and project configuration are applied before publication.
  • The rule requires neither an external analyzer nor network access.

Configuration and suppression

BSL### is the primary stable identifier. The compatible alias is accepted in select, ignore, and compatible block suppression comments.

[tool.onec-hbk-bsl]
select = ["BSL154"]
ignore = ["CodeAfterAsyncCall"]

All three suppression families support both a current line and a range. When an opening comment follows code, it affects only that line. Use any one form:

  • noqa:
Value = "example";  // noqa: BSL154
  • bsl-disable:
Value = "example";  // bsl-disable: BSL154
  • compatible BSLLS form:
Value = "example";  // BSLLS:CodeAfterAsyncCall-off

When the same opening comment is on a line by itself, it starts a range. Close it with the matching marker from the same family:

// noqa: BSL154
// code without this diagnostic
// noqa-enable: BSL154

// bsl-disable: BSL154
// code without this diagnostic
// bsl-enable: BSL154

// BSLLS:CodeAfterAsyncCall-off
// code without this diagnostic
// BSLLS:CodeAfterAsyncCall-on

To disable the rule until the end of the file, omit the closing noqa-enable, bsl-enable, or BSLLS:…-on marker.

Opening and closing markers must belong to the same family.

Description

When using asynchronous methods, developers may write lines of code follow immediately after calling the asynchronous method. In this case, the specified lines of code are executed immediately, without waiting for the asynchronous method to execute.

For the correct solution, you need to move all the code that must be executed after the asynchronous action is completed into the export method and specify its name in the notification processing that will be called after the asynchronous action completes. Or use asynchrony through promises, for example, Wait AlertAsync(Text);

Examples

Incorrect code

&AtClient
Procedure Command1(Command)
    AdditionalParameters = New Structure("Result", 10);
    Notify = New NotifyDescription("AfterNumberWereInputted", AdditionalParameters.Result, 2);

    Message("Inputed value is " + AdditionalParameters.Result); // wrong because there will always be 10
EndProcedure

&AtClient
Procedure AfterNumberWereInputted(Number, AdditionalParameters) Export
    If Number <> Undefined Then
        AdditionalParameters.Result = Number;
    EndIf;
EndProcedure;

Correct code

&НаКлиенте
Процедура Команда1(Команда)
    ДополнительныеПараметры = Новый Структура("Результат", 10);
    Оповещение = Новый ОписаниеОповещения("ПослеВводаКоличества", ЭтотОбъект);
    ПоказатьВводЧисла(Оповещение, 1, "Введите количество", ДополнительныеПараметры.Результат, 2);

КонецПроцедуры

&НаКлиенте
Процедура ПослеВводаКоличества(Число, ДополнительныеПараметры) Экспорт
    Если Число <> Неопределено Тогда
        ДополнительныеПараметры.Результат = Число;
        Сообщить("Введенное количество равно " + ДополнительныеПараметры.Результат); // неверно, т.к. всегда будет 10
    КонецЕсли;
КонецПроцедуры;

In some cases, executing code immediately after calling an asynchronous method is entirely possible if you do not need to wait for the results of the asynchronous action. For example

&AtClient
Procedure Command(Command)
    ShowMessageBox(, "Moneo te!", 10);
    Message("code started working after ShowMessageBox");
    // ...
EndProcedure

It is also important to consider that an asynchronous method can be called in one of the code branches and you need to analyze the subsequent code until the end of the current procedure\function. Example:

&НаКлиенте
Процедура Команда1(Команда)
    ДополнительныеПараметры = Новый Структура("Результат", 10);
    Если Условие Тогда
        Оповещение = Новый ОписаниеОповещения("ПослеВводаКоличества", ЭтотОбъект);
        ПоказатьВводЧисла(Оповещение, 1, "Введите количество", ДополнительныеПараметры.Результат, 2);
    Иначе
        // какой-то код
    КонецЕсли;
    // последующий код также может вызываться сразу после вызова асинхронного метода, что может быть неверно

    Сообщить("Введенное количество равно " + ДополнительныеПараметры.Результат); // неверно, т.к. всегда будет 10
КонецПроцедуры

Sources