[Pas2js] Async, AWait

warleyalex warleyalex at yahoo.com.br
Mon Jun 1 02:22:32 CEST 2020


Nice!

The Sleep method is missing in pa2js! This method is very useful, I think it
should live at sysutils unit.

-------------------------------
function Sleep(ms: NativeInt): TJSPromise;
begin
  Result := TJSPromise.New(
    procedure(resolve,reject : TJSPromiseResolver)
    begin
      window.setTimeout(
      procedure()
      begin
        console.log('Done waiting');
        resolve(ms);
      end, ms);
    end);
end;   
-------------------------------

Example usage:

function asyncResponse: TJSBlob; async;
var
   r: TJSResponse;
   j: TJSPromise;
begin
   r := await(window.fetch('https://jsonplaceholder.typicode.com/posts/1'));
   j := await(r.json());
   console.log(j);
   await(Sleep(5000));
   console.log('A');
   await(Sleep(5000));
   console.log('B');
end;    

regards,
warleyalex



--
Sent from: http://pas2js.38893.n8.nabble.com/


More information about the Pas2js mailing list