pas2js AsyncAWait

From Lazarus wiki
Revision as of 11:49, 21 May 2020 by Mattias2 (talk | contribs) (Created page with "=Overview= Pas2js supports the JS operators async/await to simplify the use of Promise, which itself simplifies writing asynchronous code, like starting a download, waiting f...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Overview

Pas2js supports the JS operators async/await to simplify the use of Promise, which itself simplifies writing asynchronous code, like starting a download, waiting for the result, then depending on the result start the next and so forth. Instead of writing a tedious and hard to read mass of sub functions an async procedure can be written in the common serial aka imperative way.

AWait

Pas2js provides two built-in await functions:

function await(const Expr: T): T; // implicit promise
function await(aType; p: TJSPromise): aType; // explicit promise requires the resolved type

The await function can only be used inside a procedure with the async modifier. For example:

Program MyModule;

uses JS, Web;

function ResolveAfter2Seconds: TJSPromise;
// returns immediately with a Promise,
// which after 2 seconds gets resolved
begin
  Result:=TJSPromise.new(procedure(resolve, reject : TJSPromiseResolver)
    begin
    window.setTimeout(procedure
      begin
      resolve('resolved');
      end,
      2000); // wait 2 seconds
    end);
end;

procedure AsyncCall; async;
var s: string;
begin
  writeln('calling');
  s := await(string,resolveAfter2Seconds());
  // the await pauses this procedure returning to the caller
  // when the Promise from resolveAfter2Seconds gets resolved 
  // this procedure is continued
  writeln(s); // expected output: 'resolved'
end;

begin
  AsyncCall;
  // calling AsyncCall returns immediately, while the Promise is waiting
  writeln('called');
end.

Expected output:

calling
called
resolved