[Pas2js] TDataset load local JSON in a async way. How?

Michael Van Canneyt michael at freepascal.org
Fri Aug 28 07:52:49 CEST 2020



On Thu, 27 Aug 2020, warleyalex via Pas2js wrote:

> In this example:
> https://pas2js.github.io/master/ProjectFishfacts/index.html
>
> I use TDataset which inherits from TBaseJSONDataSet to load a local JSON
> file.
>
> If you see at
> https://github.com/pas2js/pas2js.github.io/blob/master/master/ProjectFishfacts/ucds.pas
>
> It is possible to load synchronously but there is that warning:
> [Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated
> because of its detrimental effects to the end user's experience. For more
> help, check https://xhr.spec.whatwg.org/.
> --> FReq.open('GET',FURI, FALSE);  // async = false
>
> but would prefer TDataset to load asynchronously using the modern (async/
> await / fetch). But unfortunately, I could not find a way.

But async/await/fetch is still asynchronous. 
It's just a syntax to hide this fact. 
I fail to see how  this is 'modern'.

your function:

function fetchAsync(url: String): TJSPromise; {$ifndef
InLazIDE}async;{$endif}
var
   response: TJSResponse;
   data: TJSPromise;
begin
   response := await(window.fetch( url ));
   Result := await(response.json());

end;

seems correct. I would expect you to use it as:

MyDataset.JSON:=await(TJSobject,fetchasync('myurl'))

Do you get an error or what is the problem you are experiencing with your function ?

Michael.


More information about the Pas2js mailing list