Skip to content

Lines of code after the asynchronous method call (CodeAfterAsyncCall)

Type Scope Severity Activated
by default
Minutes
to fix
Tags
Code smell BSL Major No 10 suspicious

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

Snippets

Diagnostic ignorance in code

// BSLLS:CodeAfterAsyncCall-off
// BSLLS:CodeAfterAsyncCall-on

Parameter for config

"CodeAfterAsyncCall": false