async / await keywords
It would really awesome if DWScript could support for async functions. Await and Async are just syntactic sugar for working with promises. It would be cool if we can define a async method like:
// 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;
the compiler emits this
function main() {
(async function(){(null);
await Sleep(5000);
console.log("A after 5s");
await Sleep(3000);
console.log("B after 3s");
})();
};
Yeah, we need to to wrap it inside an async function and add "await" before the method "Sleep" to work in synchrounous manner.
Comments (6)
-
-
I think that it's better to have like this for normal procedure/function:
Procedure MyProc; async;
And for anonymous function/procedure:
OneProc(async function())...
@Eric Grange What do you propose please waiting that this feature will be implemented? Thank you.
-
Hello,
Nice that you finally implemented it. Thank you.
Regards,
-
repo owner This is currently very minimalistic support to “play” with the syntax.
Currently it’s with async in front (as in JS) but without having any effect on the return value of the function or procedure, and the compiler knows nothing about promises or async execution context.
A snippet inspired from the MDN doc where this can be used is below.
I had a look at making Promises into generics, but truth is JS libs out there would run afoul of the strong typing of a generic… So maybe a named Variant + helpers could be preferable, less strict but still providing some degree of syntax checking and auto-completion.type TPromiseExecuteResult = procedure (execute : procedure (result : Variant)); type Promise = class external 'Promise' constructor Create(p : TPromiseExecuteResult); end; function setTimeout(p : procedure; millisecs : Integer) : Integer; external; function ResolveAfter2Seconds : Promise; begin Result := new Promise( lambda (resolve : procedure(r : Variant)) setTimeout(lambda resolve('resolved') end, 2000); end ); end; async procedure asyncCall; begin PrintLn('calling'); var result := await ResolveAfter2Seconds(); PrintLn(result); // expected output: "resolved" end; asyncCall();
-
Hello @Eric Grange,
Thank you. I will try it.
Thank you for your support.
Regards,
Toky
-
Great work as always eric! This makes DWScript generated JS highly compatible with existing frameworks, and also makes it possible to write some very powerful and exciting libraries.
Thank you!
- Log in to comment
Are async/await syntaxes now supported please?