Using Await on a TJSPromise


currently trying to make first steps to create my own Miletus component for controlling the Raspberry GPIO interface. I derived it from TComponent and I thought its a good idea to use a timer for polling the GPIO. Inside the timer event I'm using the GPIORead(AGPIOPin: Integer): TJSPromise; defined in WEBLib.MiletusRaspi.pas. I marked the timer event procedure with [async] and it compiled fine. When using my control in a Miletus test app, the compiler is complaining that using Await is only allowed in async procedures. What is my mistake here?

Best regards

TMiletusRaspberryGPIO = class(TComponent)
    FPollingTimer : TTimer;
    [async]procedure OnTimer(Sender : TObject); 
    constructor Create(AOwner: TComponent); override;

constructor TMiletusRaspberryGPIO.Create(AOwner: TComponent);
    FPollingTimer:= TTimer.Create(nil);
    FPollingTimer.OnTimer:= OnTimer;

procedure TMiletusRaspberryGPIO.OnTimer(Sender : TObject);
   value : Integer;
    value:= Await(Integer, GPIORead(item.Port));

VSC/Win10/TMSWEBCoreVSC 1.9.85543


The code snippet you sent works fine here. You don't need to mark the OnTimer implementation with [async] too, but that alone is not a problem.

This has been tested in a newly created Miletus project with the same version (1.9.85543).

How are you using the component in your app? Can you provide further details or a small sample project that reproduces the issue?

I got stuck on this as well, You can also mark the procedure async by using a directive. For some reason the compiler likely does not see the attribute:
function HashForString(AString: string): string; //async;

function HashForString(AString: string): string; async;

Reference : pas2js AsyncAWait - Free Pascal wiki

Do you have more details? Do you add the [async] attribute on a class method?

Also, note that the pas2js compiler specifies to use the async decorator. In TMS WEB Core, we added the extra capability to use an attribute on the class method. The reason for this is that the attribute is a standard Pascal language feature the Delphi LSP recognizes while the async decorator extension from the pas2js compiler not.

I don't understand. I see nothing async in this code?

Ok, I believe the issue can be re-produced. In this project the BO.pas unit has the logic to compute a hash.

The attribute works when the unit is part of the project.

When the unit is not part of the project but gets found on the search path it does not compile with the attribute and the decorator must be used. (7.2 KB)

For anyone else watching this tread I removed the posts that do not add value as I have now provided a minimal sample

It is indeed a requirement for handling of the attribute that the unit is part of a project.