[JSCodeGen] ASYNC procedure inside class declaration

Issue #195 new
Toky Olivier Razanakotonarivo created an issue

Actually, ASYNC/AWAIT features are doing their job, just one thing now, I cannot use ASYNC inside a class declaration. For example:

type
  TMyClass = class(TObject)
  public
    async procedure LongTask(); //Compiler error: Syntax Error: Colon ":" expected
  end;

Can It be corrected please?

In Javascript, something like this can be done:

let obj = {};
let myObj = {
    async setObj(a,b) {
        obj.a = a;
        obj.b = b;
    }
}

And you use it like this:

async function consoleLog() {
    await myObj.setObj(2, 3);
    console.log(obj.a + obj.b);
}

consoleLog();

Thank you Eric!

Comments (4)

  1. Toky Olivier Razanakotonarivo reporter

    Finally I found It, no need to put it in the class declaration.

     TMyButton = class(TQTXButton)
        class function CreateAsync(AOwner: TQTXComponent): JPromise;
        class function fromVariant(v: Variant): TMyButton;
      end;
    
      class function TMyButton.CreateAsync(AOwner: TQTXComponent): JPromise;
      begin
        Result := new JPromise(procedure (Resolve, Reject : JPromiseResolver)
      begin
        var btn: TQTXButton := TQTXButton.Create(AOwner,
        procedure (button: TQTXButton)
        begin
          Resolve(button);
        end);
      end);
      end;
    
      class function TMyButton.fromVariant(v: Variant);
      begin
        asm @Result = @v; end;
      end;
    
      //To use them
      async procedure TForm1.AddButton();
      begin
        var ret := await TMyButton.CreateAsync(Self);
        var btn := TMyButton.fromVariant(ret);
        btn.innerHtml := 'One button';
      end;
    

    Very useful feature, thank you Eric!

  2. Toky Olivier Razanakotonarivo reporter

    Just one question, how to make an “async class function/procedure”?

  3. Log in to comment