[Pas2js] JS Sync/Async Limitations

Sergei Vertiev vertiev at gmail.com
Thu Feb 21 10:24:49 CET 2019


Hi all Pas2JS gurus!

Being Pascal/C++ programmer for decades, i am relativelly novice in JavaScript.
Ater reading about Pas2JS, i decide that it is perfect thing which give
me ability of reuse some of existing Pas files with
data classes and some logic in possible Web projects.

Spending some time and doing some afforts, i managed to compile alot of files
to JS. That was really inspiring moment.
(BTW, JS code is really perfect and easy to understand and debug).

Then i wrote NodeJs service (sure in JS), for getting data from PostgreSql DB.
In NodeJs we have to work in absolutelly async manner of programming,
but this is normal, because NodeJs works with Http Requests
and result of each request is Responce.

So, control flow is something like this:
Http Request -> do something => (Callback or Promis: continue doing
something => e.t.c => (Callback or Promise Return Http Responce))

But JS on browser side is something different!

Lets say in Delphi we have following sequence of getting Properties.

i:=Client.Phones.Count;

Assumed, that when we handle ".Phones" property,
Delphi code implements so called "lazy data loading" by
access to DB.
That is no problem with Delphi and can be implemented different ways.

But having JS classes, compiled with Pas2Js
and trying to achive the same behavior, i sudenly realize:
this is absolutelly impossible to implement at browser side!

Any call to async operation returned immediatelly,
all work of async operation performed in Callback/Promise and
there is no ways in JS to wait for result of this Callback operation.
JS interpreter (VM) simply never call to Callback code,
while you stay in some funcion (e.g. in Cycle,checking current time).
Even a WebWorkers which are known as "js threads" cant help..
When we stay in some function - we can't receive any messages from
WebWorkers or execute async code.

And there are no ways to avoid async operations.
Http Get/Post works asynchroniusly, alot of JS APIs have
only Async methods (e.g. IndexedDB)...

This is most significant difference between Delphi and JS
behavior and unfortunatelly my "experimental" project which
starts so happyly - stuck at this point.

Sure, cool JS programmers know about this specific very well.
But, i was unaware of this, and i think that many Pascal/Delphi
programmers don't know about this JS specific neither.

So, simply decide to share this knowladge with you.


More information about the Pas2js mailing list