Await in main program

Hello,

is there a way to use await in the main program file?

var
  lResult: Boolean;
begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  lResult := await(Boolean, TMyObject.MyClassProc);
  Application.Run;
end.

I just found here the video of Leon Kassebaum, where he used

uses
  JS; 
begin
  lResult := TAwait.Exec<Boolean>(TMyObject.MyClassProc);
  ...

but this does not compile with the error "identifier not found TAwait".

What I'd like to get is to load some parameters from a json file via WebRequest and after this to show the main form of my application. So I tried to do the loading in an async/await in the main program section and after that the Application.CreateForm.

Do You have any advice for this?

Kind regards
Harald

TAwait is in js.pas
Using await/TAwait needs to be done from a code block marked as async.
So, either use the attribute [async] or decorator async; with the function where you use it.

That's really strange. Look at this screenshot:

I startet a very new WebCore application and tried to compile it.

I suggest following solution:

Put the async code in a method in a separate unit that you add to your project.
Include this unit from the main DPR file and call the method from there.

Thank You, this is a solution. It means I will have a second "run"-procedure in the main program - like this:

program LoginTest;

uses
  Vcl.Forms,
  form_main in 'form_main.pas' {Form5: TWebForm} {*.html},
  uTest1 in 'uTest1.pas';

{$R *.res}

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;

  TMyApplicationRunner.Run;

//  Application.CreateForm(TForm5, Form5);
  Application.Run;
end.

and in the separate unit

unit uTest1;

interface

uses
  Vcl.Forms, WEBLib.Forms,
  JS, Web,
  form_main;

type
  TMyApplicationRunner = class
  public
    [async]
    class procedure Run;
    [async]
    class function DoSome(var lForm2): string;
  end;

var
  lForm: TForm5;

implementation

class function TMyApplicationRunner.DoSome(var lForm2): string;
begin
  Result := 'Hallo';
end;

class procedure TMyApplicationRunner.Run;
var
  lResult: string;
begin
  lResult := await(string, DoSome(lForm));
  Application.CreateForm(TForm5, lForm);
end;

end.

1 Like