I need a sample example of how to use AsyncSleep

I need a sample example of how to use AsyncSleep. please provide me.

// Rtl.HTMLUtils // webutils
function AsyncSleep(ms: NativeInt): TJSPromise;

Defined a Sleep function.

Calling the following will produce 1 > 2 > 3 Logs.
I want to run so that 1 > 3 > 2 logs are created.
I don't know what to do.
Please tell us.

1> Start-Sleep
2> End-Sleep
3> last 1sec

console. log('Start - Sleep');
Sleep(1000,
procedure
begin
console.log('last 1sec');
end);
console. log('End - Sleep');

procedure Sleep(ms: Integer; ACallback: TProc);
var
Promise: TJSPromise;
begin
Promise := TJSPromise.new(
procedure(resolve, reject: TJSPromiseResolver)
begin
window.setTimeout(
procedure
begin
resolve(nil);
end,
ms);
end);

Promise._then(
TJSPromiseResolver(
procedure(value: JSValue)
begin
if Assigned(ACallback) then
ACallback;
end
)
);
end;

In the method/function where you use AsyncSleep(ms) did you

  1. decorate this method with [async]
  2. use it like
  TForm3 = class(TWebForm)
    WebButton1: TWebButton;
    [async]
    procedure WebButton1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form3: TForm3;

implementation

{$R *.dfm}
uses
  WebUtils;

procedure TForm3.WebButton1Click(Sender: TObject);

begin
  console.time('a');
  await(boolean,asyncsleep(1000));
  asm
  console.timeLog('a');
  end;
end;

you'll see in the browser console log that approx. 1sec was waited.

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