Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / pas2js / 2020-05

Async, AWait

11 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Mattias Gaertner

Hi all,

pas2js trunk now supports async procedures and await functions. This can greatly simplify writing asynchronous code, like starting a download, handle the result, depending on the result download the next piece and so forth.
Formerly you needed some (anonymous) functions for that. Now you can write asynchronous code, that looks like normal, synchronous code.

https://wiki.freepascal.org/pas2js_AsyncAWait

Mattias

Reply by email

Michael Van Canneyt
to Mattias Gaertner
Mattias Gaertner wrote 11 lines
Hi all, pas2js trunk now supports async procedures and await functions. This can greatly simplify writing asynchronous code, like starting a download, handle the result, depending on the result download the next piece and so forth. Formerly you needed some (anonymous) functions for that. Now you can write asynchronous code, that looks like normal, synchronous code. https://wiki.freepascal.org/pas2js_AsyncAWait

I have added the demos from the wiki page to the demo dir in the sources, fixed a small error in one of the demos.

Enjoy !

Michael.

Reply by email

silvioprog
to Mattias Gaertner: pas2js trunk now supports async procedures and await functions. This can greatly simplify…

On Thu, May 28, 2020 at 4:17 AM Mattias Gaertner via Pas2js <

Show quoted text 15 lines
wrote: Hi all, pas2js trunk now supports async procedures and await functions. This can greatly simplify writing asynchronous code, like starting a download, handle the result, depending on the result download the next piece and so forth. Formerly you needed some (anonymous) functions for that. Now you can write asynchronous code, that looks like normal, synchronous code. https://wiki.freepascal.org/pas2js_AsyncAWait Mattias

This is great news! 🎉

I'm away from front-end development a bit, but I'm going to test this feature. Async/await is a very useful feature, I have been using it a lot to call non-blocking functions in Rust with more syntax sugar.

Thanks for releasing it!

Silvio Clécio -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.freepascal.org/pipermail/pas2js/attachments/20200528/0ec59ae8/attachment.htm>

Reply by email

silvioprog
to Michael Van Canneyt: I have added the demos from the wiki page to the demo dir in the sources, fixed a small…
AM Michael Van Canneyt wrote 20 lines
On Thu, 28 May 2020, Mattias Gaertner via Pas2js wrote: > Hi all, > > pas2js trunk now supports async procedures and await functions. > This can greatly simplify writing asynchronous code, like starting a > download, handle the result, depending on the result download the next > piece and so forth. > Formerly you needed some (anonymous) functions for that. Now you can > write asynchronous code, that looks like normal, synchronous > code. > > https://wiki.freepascal.org/pas2js_AsyncAWait I have added the demos from the wiki page to the demo dir in the sources, fixed a small error in one of the demos. Enjoy ! Michael.

I have a question.

Is it possible to use parameters-less at the await part? For example:

function foo: TJSBlob; *async*;
var
  r: TJSResponse;
begin
  r := *await* window.fetch(url);
  ...
  Result := *await* r.blob();
end;

Thank you!

Silvio Clécio -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.freepascal.org/pipermail/pas2js/attachments/20200528/f8e0c2d9/attachment.htm>

Reply by email

Michael Van Canneyt
to silvioprog
silvioprog wrote 39 lines
On Thu, May 28, 2020 at 5:12 AM Michael Van Canneyt wrote: > On Thu, 28 May 2020, Mattias Gaertner via Pas2js wrote: >> Hi all, >> >> pas2js trunk now supports async procedures and await functions. >> This can greatly simplify writing asynchronous code, like starting a >> download, handle the result, depending on the result download the next >> piece and so forth. >> Formerly you needed some (anonymous) functions for that. Now you can >> write asynchronous code, that looks like normal, synchronous >> code. >> >> https://wiki.freepascal.org/pas2js_AsyncAWait > > > I have added the demos from the wiki page to the demo dir in the sources, > fixed a small error in one of the demos. > > Enjoy ! > > Michael. I have a question. Is it possible to use parameters-less at the await part? For example: function foo: TJSBlob; *async*; var r: TJSResponse; begin r := *await* window.fetch(url); ... Result := *await* r.blob(); end; Thank you!

The fetch/blob example exists and works as-is.

Michael.

Reply by email

warleyalex
to Mattias Gaertner: pas2js trunk now supports async procedures and await functions. This can greatly simplify…

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

Reply by email

Michael Van Canneyt
to warleyalex
warleyalex wrote
Nice! The Sleep method is missing in pa2js! This method is very useful, I think it should live at sysutils unit.

A nice idea.

It can't live in sysutils, because it depends on web.pas (window object) and sysutils cannot depend on web.pas.
I added a new unit 'webutils.pas' and I put it in that.

I have some other ideas for functions to add to this unit, and if you have more ideas for utility routines for web.pas, please share them.

Michael.

Reply by email

Mattias Gaertner
to Michael Van Canneyt
Show quoted text 8 lines
On Sun, 31 May 2020, warleyalex via Pas2js wrote: > Nice! > > The Sleep method is missing in pa2js! This method is very useful, I > think it should live at sysutils unit. A nice idea.

Maybe choose another name?
Sleep() without await will return immediately and code ported from Delphi/FPC won't notice the problem.

Mattias

Reply by email

Michael Van Canneyt
to Mattias Gaertner
Mattias Gaertner wrote 15 lines
On Mon, 1 Jun 2020 10:17:07 +0200 (CEST) Michael Van Canneyt wrote: > On Sun, 31 May 2020, warleyalex via Pas2js wrote: > > > Nice! > > > > The Sleep method is missing in pa2js! This method is very useful, I > > think it should live at sysutils unit. > > A nice idea. Maybe choose another name? Sleep() without await will return immediately and code ported from Delphi/FPC won't notice the problem.

Well, they need to add the webutils unit, so they will need to be aware. But as a precaution, I changed the name to asyncsleep.

Michael.

Reply by email

Michael Schnell
to Mattias Gaertner: pas2js trunk now supports async procedures and await functions. This can greatly simplify…

Great !
I seem to remember that functions like that are available in Oxygen (inherited from C#) but supposedly with different names and usage conventions. It would be great if the "Desktop fpc would have this, as well to allow for "asynchronous loops" (making use of multiple CPUs) and similar stuff, and if the Pascal dialects could allow for writing compatible code for all of them. -Michael

Reply by email