[Pas2js] JS Sync/Async Limitations

Michael Van Canneyt michael at freepascal.org
Thu Feb 21 15:44:17 CET 2019



On Thu, 21 Feb 2019, warleyalex via Pas2js wrote:

> I don't know you, it would be really cool to have await keyword native in
> pas2js.
> the "async" keyword to allow us to program using asynchronous requests in a
> "synchronous" manner.
>
> in modern JS:
>
> async function r() {
>  var r2 = await fetch('https://jsonplaceholder.typicode.com/posts/1');
>  var j = await r2.json();
>  console.log(j);
> };
> r();

While I agree it could be a nice to have, it just moves the problem of the
Original Poster...

Example of MDN:

function resolveAfter2Seconds() {
   return new Promise(resolve => {
     setTimeout(() => {
       resolve('resolved');
     }, 2000);
   });
}

async function asyncCall() {
   console.log('calling');
   var result = await resolveAfter2Seconds();
   console.log(result);
   // expected output: 'resolved'
}

asyncCall();
console.log('called');

Results in

> "calling"
> "called"
> "resolved"

As you can see, the asynccall returns at once.


Michael.


More information about the Pas2js mailing list