background

In the 1960s, the single operating unit in an operating system was usually a process. However, with the development of computer technology, people find that in the process of running, it costs a lot of time and space to create, undo and switch.

In the 1980s, to solve this problem, there was a smaller basic unit of independent operation called the thread.

Operating system CPU time was divided into many smaller time slice, in each individual time slice to execute commands a thread, to the next time to continue the next thread’s instructions, each thread execution in turn, because every time time is short, all threads can run, as if all the threads at the same time for users. The end result is that multiple threads can be created during programming, running at the same time, and each thread can run “in parallel” to accomplish different tasks.

At this time, a new problem also appears, in the single-thread running mode, when one piece of code calls another piece of code, can only use synchronous call, only after the current code has completed the execution of the result, the call can continue to execute. As an example, there is now only one water tank, and a horse can only drink water after another horse has left.

With the support of multithreading, asynchronous function can be used to call, this problem is solved.

Introduction to asynchronous functions

There will be a lot of content in the program, the calculation content is complex, the rendering content is various, and the processing process needs to spend more time. When A module A calls the processing content of module B, the content in module B will need some time to process. At this time, if module A keeps waiting, the program performance will be seriously affected. In actual situation, such as in the front page to fill in the online data processing, after need to calculate the content of the data in the table shows, this is because the calculation is not completed, the page content is not show, give the user the feeling is the content click run, but has no feedback page.

After the asynchronous function is called, modules A and B are executed on different threads.

In an asynchronous call, module A does not need to wait for module B to return the content before continuing to execute the subsequent code.

After the execution of the contents in module B, it will notify module A: I finished processing, you remember to deal with the subsequent contents.

With asynchronous invocation, we can optimize the display problem we just mentioned in front of the page: put the entire initialization process into a single thread, and the main thread starts the thread and then goes down, making the main window instantly visible. By the time you think about the content of the operation that needs to be done, the processing of the data is already being done secretly; After the program starts to run steadily, asynchronous calls can further optimize the process of human-computer interaction. When the user clicks the mouse to operate, the operation content is time-consuming and the system does not respond immediately after the click, which makes the user experience very bad. The more time-consuming and slower operation content is converted to asynchronous call, so that the main thread is waiting for the next message at any time, so that the response speed of the user’s mouse operation action is faster, and the use experience is greatly improved.

Practice: fancy use by expert users

Examples demonstrate

Let’s use a simple example to see how asynchronous functions can be used in a front-end spreadsheet cell calculation.

var ServerDecode = function () {};
ServerDecode.prototype = new GC.Spread.CalcEngine.Functions.AsyncFunction("DECODE", 1, 255);
ServerDecode.prototype.evaluateAsync = function (context, arg1) {
    $.get("decode/" + arg1, function (data, status) {
        context.setAsyncResult(data);
    });
};

spread.addCustomFunction(new ServerDecode());

sheet.setFormula(0, 1, '=DECODE(A1)');



Copy the code

In this algorithm, we will set the calculation and analysis method part on the server, the method name is called DECODE

Next, send the parameters to the server with jquery.get request, and then get the request content to complete the setting

The entire asynchronous function is then registered into Spread

Finally, in cell B1, type DECODE(A1)

In this way, when the content of cell A1 changes, B1 will recalculate into the corresponding content according to the calculation rules set by us

Fancy use of asynchronous functions

Tools have always been used in different hands, and last winter we received feedback from our users about the wonderful ways in which asynchronous functions can be used.

They combine the parameters of the asynchronous function into a SQL that is sent to the database for data query and display the query results at the end of the query. Everything turned out to be right, but there was one small problem.

In the process of use, the user found that the query was more than four times in the whole process, and asked us if the formula was wrong?

We immediately carried out troubleshooting, in the process of looking at the source code, we found that in order to emphasize the importance of data when this function was first implemented, when there are multiple asynchronous function calls in the same formula, we will calculate the content of the asynchronous function that has been calculated again when calculating the next content.

I didn’t expect the user to actually use asynchronous functions this way, and the whole thing was tweaked later. It is now adjusted to only evaluate asynchronous functions once per call.

Given this experience, and other fancy uses of asynchronous functions, it’s no surprise.

Sure enough, it wasn’t long before we received feedback from other users.

This time the user uses an asynchronous function to fetch the current service name from the server and display it in SpreadJS.

We found that the user also added a format string to it to get the user’s QR code. At the same time, there is also a conditional format, if the user does not log in there will be an error message.

This is a short example, but here the user uses asynchronous functions, conditions, formats, and format strings all in one.

conclusion

The above is all the background and principle of the birth of asynchronous functions, as well as the use of asynchronous functions and various fairy users in the front-end spreadsheet fancy use, to this section on the principle of spreadsheet calculation of all the content has been introduced.

Think the content is good to give a thumbs-up before you go ~