[Effective JavaScript Notes] Chapter 7th: Concurrency--Personal summary

Source: Internet
Author: User
Tags rounds

Objective

This chapter learns about event queues and asynchronous APIs. JS is just a scripting language that runs in other applications. JS is dependent on the application, also independent with the application program. Allows it to run on multiple platforms and environments. There is no description of concurrency in the ECMAScript standard. This chapter discusses some common methods that use events and asynchronous APIs as the basic part of JS programming. Asynchronous API, with Settimeout,setinterval. 61st: Do not block the I/O event Queue Personal Summary

JS is a single-threaded language built on top of events. The JS processing interaction is transmitted by the method of the event, and the handler function of the listener event is based on the event queue to execute the corresponding listener function. Synchronous processing

Synchronous processing, in an I/O request, waits for the input content. If there is no input, it waits until the input has the result. This time, in multi-threaded language can open another thread processing other calculations, but JS is a single-threaded language, can only wait, that is, blocked, wasting computer resources. Asynchronous processing

In JS, you can provide a callback function for an I/O operation, and the program will continue to process the following code. The callback function does not perform a related operation until the I/O has input. This is implemented by the attributes of the event queue, so that no blocking code can be implemented. Tips

    • Asynchronous APIs use callback functions to slow down processing expensive operations to avoid blocking the main application

    • JS receives the event concurrently, but uses an event queue to process the event handler sequentially

    • Never use blocked I/O in the Application event queue

62nd: Using nested or named callback functions in an asynchronous sequence personal summary

The simplest way to understand an action sequence is to initiate an asynchronous API rather than perform an operation. So the code after the initiating operation executes first, and the registered event handler executes in the next round of the event loop. Concatenation of completed asynchronous operations can be done using nested methods. But too many layers can cause code to be messy and long. Reduce nesting methods: Use a named function to bind using the Bind method. The combination of these methods can be a better solution to the problem. Tips

    • Use nested or named callback functions to execute multiple asynchronous operations sequentially

    • Try to strike a balance between too many nested callback functions and awkward named non-nested callback functions

    • Avoid sequencing operations that can be executed in parallel

63rd: Beware of discarding mistakes personal summary

Managing asynchronous programming, debugging is not easy, where errors occur and where errors are caught are poorly positioned. Here, the asynchronous operation passes the wrong information in the form of a callback function parameter to the outer layer. The error is handled in the callback function so that the code can run correctly. Tips

    • Avoid copying and pasting error-handling code by writing shared error-handling functions

    • Ensure that all error conditions are handled explicitly to avoid discarding errors

64th: Recursive personal summary of the use of asynchronous loops

Asynchronous loops, as 62 says, the loop here only initiates multiple operations at the same time, but does not perform the operation. Therefore, the loop cannot be aborted in the execution operation. The loop operation is rewritten as a recursive function, and the asynchronous initiation and execution are serialized. But there is a new problem, the JS environment usually holds a fixed area in memory, called the call stack, to record what to do next before the function call returns. This is a stack of the way to store the "advanced back out." However, if such an excessive number of calls, will cause the stack space is exhausted, will eventually throw an exception, that is, the stack overflow. Tips

    • The loop cannot be asynchronous

    • Use recursive functions to perform iterations in separate rounds of an event loop

    • Recursion is performed in a separate turn in the event loop and does not cause the call stack to overflow

65th: Do not block the event queue when calculating personal summary

The 61st article explains how the asynchronous API prevents a program from blocking an application's event queue. An operation in the event queue cannot be performed if a normal execution code has been consuming threads. During this time in the browser environment, cannot respond
User action. The worker API of the Web client platform can handle the calculation of pure data, thus preventing blocking of the event queue during computation. Tips

    • Avoid executing expensive algorithms in the main event queue

    • In a platform that supports the worker API, the API can be used to run long computing programs in a separate event queue

    • In environments where the worker API is unavailable or expensive, consider factoring the computational program into multiple rounds of the event loop

66th: Using counters to perform parallel operations personal summary

When dealing with multiple concurrency, you cannot take care of the order of the parameters in the callback function. Causes the post code to not run correctly, for each initiation operation is counted once, returns the corresponding return results to record. The callback function then processes the result of the record. You can guarantee that the program will run as scheduled steps. Tips

    • The occurrence of events in the JS application is indeterminate, that is, the order is unpredictable

    • Use counters to avoid data contention in parallel operations

67th: Never call asynchronous callback functions synchronously personal summary

The result of the asynchronous return, which can be saved in a cache. In this case, the data in the cache can be used in a multi-file simultaneous download, so the callback function can be executed synchronously. But just like the call stack on 64 has the potential for problems that can cause stack space to run out. The callback function also uses an asynchronous call, using SetTimeout to invoke the corresponding callback function. Tips

    • Never call an asynchronous callback function synchronously, even if you can get the data immediately

    • A synchronous call to an asynchronous callback function disrupts the expected sequence of operations and may lead to unexpected interleaved code

    • Calling asynchronous callback functions synchronously can result in stack overflow or incorrectly handling exceptions

    • Use asynchronous APIs, such as the settimeout function, to dispatch asynchronous callback functions to run in another round

68th: Clean asynchronous logic with promise mode personal summary

Using the Promise mode, you can rewrite multiple layers of nested functions into a way to synchronize incoming callback functions. This allows you to manipulate them using a variety of tool functions. such as Then,when,join,select and so on. Tips

    • Promise represents the final value, which results when the parallel operation is completed

    • Using promise to combine different parallel operations

    • Use promise-mode APIs to avoid data contention

    • Use Select (also known as choose) when required for intentional competitive conditions

Summarize
    • asynchronous invocation, using event queuing to prevent blocking

    • Multiple recursive calls to the function, the call stack may be exhausted

    • Pure computing, using the Web client's worker API

    • Using counters, you can guarantee the order of the results asynchronously

    • Using a tool framework that handles asynchronous calls

    • Can prevent data from competing

[Effective JavaScript Notes] Chapter 7th: Concurrency--Personal summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.