Jump to content
Sign in to follow this  
warleyalex

async / await keywords

Recommended Posts

look this following procedure: 

procedure main; 
begin 
  Sleep(5000); 
  console.log('A'); 
  Sleep(3000); 
  console.log('B'); 
end; 

when we run this code, it will immediately output 'A' and 'B' - JS the asyncronous nature. ...but I want our program to log 'A' after 5 seconds then log 'B' after 3 seconds...  that why we, we need the async / await native keywords in SMS. In a "synchronous" manner.  Await and Async are just syntactic sugar for working with promises. It would be cool if we can declare a method like:

procedure main; async; 

The compiler can check whether the encompassing function has async() in then and emit expectd JS, for instance:

   (async function(){

   })();

Yeah, we need to to wrap it inside an async function and add "await" before the method "Sleep" to work in synchrounous manner.
ther await keyword native in SMS would be nice.

await Sleep(5000);

 

I found out an ugly workaround for the "Immediately Invoked Function Expression (IIFE) -  async function" to work at SMS.

  { global functions }
  procedure async(fn: Variant = nil); external '(async function(){' ;
  procedure &end; external '})';
  function await(promise: Variant): JPromise; external 'await ';

function Sleep(ms: Integer): JPromise;
begin
  Result := JPromise.Create(
    procedure(resolve,reject : TCallBack)
    begin
      window.setTimeout(
      procedure()
      begin
        console.log('Done waiting');
        resolve(ms);
      end, ms);
    end);

procedure main;
begin
  async;
    await( Sleep(5000) );
    console.log('A after 5s');
    await (Sleep(3000));
    console.log('B after 3s');
  &end;
end;
end;

the compiler will emit expected JS at least:

function main() {
   (async function(){(null);
   await (Sleep$1(5000));
   console.log("A after 5s");
   await (Sleep$1(3000));
   console.log("B after 3s");
   })();
};

but it would super awesome if we could

// define procedure as async function
  procedure main: async;

procedure main;
begin
  await Sleep(5000);
  console.log('A after 5s');
  await Sleep(3000);
  console.log('B after 3s');
end;

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×