async await not working as I expected

I have two procedures proc1 and proc2 both marked [async]
In my method I am calling first proc1 and then proc2. But proc2 returns before proc1.

I am lost.

This is my code:
TdmMain = class(TWebDataModule)
private
{ Private declarations }
[async] procedure LoadAppConfig;
[async] procedure LoadBrand(ABrandId: string);
procedure DoLivePerson;
public
{ Public declarations }
AppConfig: TAppConfig;
Session: TUserSession;
procedure LoadStoreListByBrand;
procedure DoAppInit;
end;

//this is the first proc
procedure TdmMain.LoadAppConfig;
var
WebHTTPRequest: TWebHTTPRequest;
request: TJSXMLHttpRequest;
json: string;
javascriptobject: JSValue;
begin
WebHTTPRequest := TWebHTTPRequest.Create(nil);
try
WebHTTPRequest.Headers.Append('Cache-Control=no-cache');
WebHTTPRequest.URL := 'appconfig.json';
request := await(TJSXMLHttpRequest, WebHttpRequest.Perform());
json := string(request.response);
javascriptobject := TJSJSON.parse(json);
AppConfig := TAppConfig(javascriptobject);
finally
WebHTTPRequest.Free;
end;
end;

//this is the second
procedure TdmMain.LoadBrand(ABrandId: string);
var
WebHTTPRequest: TWebHTTPRequest;
request: TJSXMLHttpRequest;
json: string;
javascriptobject: JSValue;
begin
WebHTTPRequest := TWebHTTPRequest.Create(nil);
try
WebHTTPRequest.Headers.Append('Cache-Control=no-cache');
WebHTTPRequest.URL := AppConfig.appBaseUrl + '/api/Brand/Get?BrandId=' + ABrandId;
request := await(TJSXMLHttpRequest, WebHttpRequest.Perform());
json := string(request.response);
javascriptobject := TJSJSON.parse(json);
Session.Brand := TBrand(javascriptobject);
finally
WebHTTPRequest.Free;
end;
end;

procedure TdmMain.DoAppInit;
var
brandIdPrm: string;
appTypePrm, actionPrm: string;
appTypeInt: Integer;
begin
LoadAppConfig;
if HasQueryParam('brandId', brandIdPrm) then
LoadBrand(brandIdPrm);

//loadBrand returns before
end;

Sorry for the previous unformatted code, here is the code:

This is my code:
  TdmMain = class(TWebDataModule)
  private
    { Private declarations }
    [async] procedure LoadAppConfig;
    [async] procedure LoadBrand(ABrandId: string);
    procedure DoLivePerson;
  public
    { Public declarations }
    AppConfig: TAppConfig;
    Session: TUserSession;
    procedure LoadStoreListByBrand;
    procedure DoAppInit;
  end;

//this is the first proc
procedure TdmMain.LoadAppConfig;
var
  WebHTTPRequest: TWebHTTPRequest;
  request: TJSXMLHttpRequest;
  json: string;
  javascriptobject: JSValue;
begin
  WebHTTPRequest := TWebHTTPRequest.Create(nil);
  try
    WebHTTPRequest.Headers.Append('Cache-Control=no-cache');
    WebHTTPRequest.URL := 'appconfig.json';
    request := await(TJSXMLHttpRequest, WebHttpRequest.Perform());
    json := string(request.response);
    javascriptobject := TJSJSON.parse(json);
    AppConfig := TAppConfig(javascriptobject);
  finally
    WebHTTPRequest.Free;
  end;
end;

//this is the second
procedure TdmMain.LoadBrand(ABrandId: string);
var
  WebHTTPRequest: TWebHTTPRequest;
  request: TJSXMLHttpRequest;
  json: string;
  javascriptobject: JSValue;
begin
  WebHTTPRequest := TWebHTTPRequest.Create(nil);
  try
    WebHTTPRequest.Headers.Append('Cache-Control=no-cache');
    WebHTTPRequest.URL := AppConfig.appBaseUrl + '/api/Brand/Get?BrandId=' + ABrandId;
    request := await(TJSXMLHttpRequest, WebHttpRequest.Perform());
    json := string(request.response);
    javascriptobject := TJSJSON.parse(json);
    Session.Brand := TBrand(javascriptobject);
  finally
    WebHTTPRequest.Free;
  end;
end;

procedure TdmMain.DoAppInit;
var
  brandIdPrm: string;
  appTypePrm, actionPrm: string;
  appTypeInt: Integer;
begin
  LoadAppConfig;
  if HasQueryParam('brandId', brandIdPrm) then
    LoadBrand(brandIdPrm);

//loadBrand returns before
end;

You'll need to make LoadAppConfig return a promise and use it then like

await(LoadAppConfig)
if ...
in an async DoAppInit method

Thanks.. I converted to this version that returns a promise and seems procedures now return in expected corrrect order.

function TdmMain.LoadAppConfig: TJSPromise;
var
  p: TJSPromise;
  JSObj: JSValue;  
begin
  Result := TJSPromise.new(
    procedure(ASuccess, AFailed: TJSPromiseResolver)
    begin
      WebHTTPRequest.URL := 'appconfig.json';
      WebHttpRequest.Execute(
        procedure(AResponse: string; ARequest: TJSXMLHttpRequest)
        begin
          JSObj := TJSJSON.parse(AResponse);  
          ASuccess(TAppConfig(JSObj));     
        end
      );        
    end
  );
end;

This topic was automatically closed 60 minutes after the last reply. New replies are no longer allowed.