Jump to content

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;

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...