Difference between revisions of "pas2js AsyncAWait"

From Lazarus wiki
Jump to navigationJump to search
(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...")
 
Line 10: Line 10:
 
function await(aType; p: TJSPromise): aType; // explicit promise requires the resolved type
 
function await(aType; p: TJSPromise): aType; // explicit promise requires the resolved type
 
</syntaxhighlight>
 
</syntaxhighlight>
The await function can only be used inside a procedure with the async modifier.
+
 
For example:
+
The await function can only be used inside a procedure with the '''async''' modifier.
 +
 
 +
An '''async''' procedure can contain multiple '''await''' calls.
 +
 
 +
==Example Wait 2 Seconds==
  
 
<syntaxhighlight lang="pascal">
 
<syntaxhighlight lang="pascal">

Revision as of 09:48, 22 May 2020

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.

An async procedure can contain multiple await calls.

Example Wait 2 Seconds

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