Unlike VFP, javascript has the ability to have multiple threads running at the same time built into it. This keeps the UI responsive while tasks are taking place in the background. In VFP, if we give a time consuming SQL query, we find that VFP stop responding till the query is complete.

Generally asynchronous commands in javascript are for
1) Querying the Web Server for some information / Sending updates to the Web Server. This is via AJAX
2) Querying a local database.
In both cases, the process can take a few seconds. The general method is to call the required task and also to pass on 2 functions to it. The first function is the process to run on sucessful completion of the task. The second function is what to do if the task fails.

$.ajax({type: "POST",url: '',data: 'SomeVariable=SomeValue', success:OnSuccess,error:OnError});
function OnSuccess(resultstring,textStatus,jqXHR){
function OnError(jqXHR, textStatus, errorThrown){
alert('Error in AJAX:'+textStatus);}

here you are passing a POST request to with some data. This should normally take some time to execute. Once the data is returned sucessfully, the function OnSuccess will run. If there is an error, the function OnError will run. (Do not worry about the ajax syntax right now. I will be dealing with that later)

Why Asynchronous is different to what we are used to

In VFP, we would tend to do something like

FOR I=1 to 20
send data to server
Do ProcessEndStatements

In asynchronous this method is not valid. Your ProcessEndStatements will run before the 20 "send data to server" are run. Also we cannot assume that the "send data to server" will run same order as the are send. You may find statement 20 running before statement 15.
Instead of that, you may want to just run statement 1. In the success of statement 1 run statement 2. In the success of statement 2 run statement 3. And so on until you reach statement 20. In the success of statement 20, you can run the ProcessEndStatements

One Response to Javascript Vs VFP III Asynchronous

  • josip Zohil says:

    “In VFP, if we give a time consuming SQL query, we find that VFP stop responding till the query is complete”.
    Try this experiment:
    1) Create a program with a long running query: Select * from largeData into cursor xxx. Put this program in a VFP project (long.pjx) and compile it in long.exe.
    2) Run your application, for example test.exe.
    3) After that run long.exe.

    The two exes are running in parallel and VFP respond before the query stop. Try this on a slow connection (for example, wi-fi) and a dual core workstation. Long.exe can execute a minute or two (in Windows Task Manager you can see the network transport of 10MB/s. It oscillates). Test.exe responds before Long is complete.

    If you run the two programs Test and Long in the VFP command window, then “…VFP stop responding till the query is complete”.

    You have two processes (two exes). They don’t interact (have no side- effects). (If they interact, there is another story). The operating system scheduler is managing the two processes. We have no guarantee how the OS execute the two processes. If the processes are simple (small…) and independent I can at least statistically predict theirs execution path ( for example, the first will end in 0.1 second, the next in 0.3 second.

    From a VFP point of view a totally new problem is the coordination of this two independent processes. This is a bit more complicated.

    Best regards Josip Zohil

Leave a Reply

Your email address will not be published. Required fields are marked *