Async keyword in TMS WEB Core (Leon Kassebaum)
Async? What’s that? Maybe you have already heard about the two concepts of synchronous and asynchronous processing of code statements. I give you an example. Let’s have a look at a “normal” Delphi application and the following lines of code: procedure MyProc(); var lResponse : string; begin lResponse := ExecuteServerRequest; lResponse := ‘Response: ‘ + lResponse; Writeln(lResponse); end; I think you know this type of application (maybe a VCL or FMX app) and of course line 6 is executed after receiving the servers response. This behavior is called synchronous because the computer performs the written code line by line. If you write the same type of code in JavaScript things are working differently. Let me explain why. First of all pure JavaScript is single-threaded. Due to that fact it only has the possibility to perform your code line by line, so to say JS code is executed synchronously. But this only applies to JS and not for browser api calls. Imagine, if you perform an http request as I did in the example above, you call functions from the browser api. At this point the JS engine can continue with the next statement (next line of code) and the browser performs his task in the background. As soon as he is ready he gives you a callback. This is due to the fact that a website always has to react e.g. for resizing. If this would not be possible you would see an hourglass when connecting to a webserver (which lasts a bit) and your whole web app would be frozen. I think you know the well known Blue circle of death from windows. The reason in JS style So what to do if you want to connect to a webserver? The thing is that you have to wait for the response until you can continue your code. In JS the answer is splitted to three statements: async, await and promises. The concept behind this is that the promise which executes your code (e.g. pulling a webserver), has two callback functions. One is called if everything was successful and the other handles possible errors. Maybe it is better to give you a tiny example in JS: … let lPromise = new Promise( function(ASuccess, AFailed){ setTimeout ( function(){ ASuccess(“Waited 2 seconds”) ; }, 2000); }); … I created a promises which performs setTimeout() to wait for 2 seconds and then he calls the success function. The next step is that you can mark a function as async so that it returns a promise and no longer maybe a string. This … async function MyAsyncFunction(){ return “Hello World!” ; } … is the same as … async function MyAsyncFunction(){ return new Promise( function(ASuccess, AFailed){ ASuccess(“Hello World!”); }); } … I know this is hard to understand but please keep in mind that this is just another way to return a promise but it is much more legible. Calling this function with the await keyword makes sure that your code waits for the success callback function of the returned promise and calculates the result of this function which can be the response of the server. Think of the lPromise from above which simply waits 2 seconds. If I would run the following lines of code console.log(“Before calling”); let lPromise = new … console.log(await […]
