[Pas2js] JS Sync/Async Limitations

warleyalex warleyalex at yahoo.com.br
Sun Feb 24 19:47:48 CET 2019


An ugly workaround for the "Immediately Invoked Function Expression (IIFE) -
async function":

function Sleep(ms: NativeInt): TJSPromise; // this should lives at the
SysUtils unit
begin
  Result := TJSPromise.New(
    procedure(resolve,reject : TJSPromiseResolver)
    begin
      window.setTimeout(
      procedure()
      begin
        console.log('Done waiting');
        resolve(ms);
      end, ms);
    end);
end;  

  { global functions }
  procedure async(result: JSValue = nil); external name '(async function(){'
;
  procedure &end; external name '})';
  function await(promise: JSValue): TJSPromise; external name 'await ';

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

will emit this JS:

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



--
Sent from: http://pas2js.38893.n8.nabble.com/


More information about the Pas2js mailing list