Async
Using Asynch with functions in EWB
EWB has a reserved word called async which is very different from JavaScript’s async functions.
EWB’s async is used to tell the compiler to run code at a future convenient time.
So if you have code like:
function TForm1.Edit1Change( Sender : TObject);
begin
async UpdateMyTables( 1, 2, 3 );
UpdateData;
end;
The function tells the Browser to wait to UpdateMyTables() until it is
convienent to do so, sometime after calling UpdateData and returning.
Since EWB code is usually event driven, this happens
after the current code completes and exits.
Unless you use WebWorkers, or other advanced topics, EWB’s JavaScript code runs to completion and does not use threads or other concurrency.
Modern JavaScript does allow a form of blocked concurrency - where it switches away from the current position and returns to it later when convenient, which looks like concurrency through promises, but really isn’t true concurrency.
However, this is not that. And EWB does not support the important JavaScrypt ASYNC function type which would allow that.
So, what is happening here. How does it scedule it for later?
Effectively, EWB uses SetTimeout() to set a timer to call the function in zero milliseconds. So when the current operation exists, JavaScript calls its scheduling function which then quickly calls all pending operations including UpdateMyTables.
One practical use for async is when you are in an event handler and doing something like wanting to free or use the resource that is calling you.
For example: if you want to reuse a resource like a TServerRequest for a second purpose, you should not set a new URL and call its Execute within the OnComplete handler or things may get confused.
So use async to schedule a call to a function which will set the new URL and call the Execute function when the current handler is done.