One ,callback Callback function

It's a function f1 And the function f2 The relationship is f1(f2());

f2 As f1() Callback function for , stay f1 Execution starts during execution f2, Execute the main logic of the thread first , Put more time-consuming tasks in the back .

The advantage of callback function is simple 、 Easy to understand and deploy

The disadvantage is not conducive to code reading and maintenance , The parts are highly coupled (Coupling), The process will be messy , And each task can only specify one callback function .

Two , Event monitoring

Event monitoring has nothing to do with the order of the code , It just depends on whether or not the event happened , Such as f1.bind(“click”,f2) Even when f1 The quilt of click when f2 perform . An event can be bound to multiple listening functions .

The whole program has to be event driven , The running process will become very unclear

3、 ... and Signal release / subscribe

jQuery.publish(“done”) It means ,f1 After execution , towards “ Signal center ”jQuery Release “done” The signal , triggering f2 Implementation .

Besides ,f2 After execution , You can also unsubscribe (unsubscribe).

jQuery.unsubscribe(“done”, f2);

The nature of this method and “ Event monitoring ” similar , But obviously better than the latter . Because we can check “ Message center ”, Know how many signals exist 、 How many subscribers per signal , So as to monitor the operation of the program .

Four 、Promises object

Promises The object is CommonJS A specification proposed by the working group , The purpose is to provide a unified interface for asynchronous programming .

In short , Its idea is , Each asynchronous task returns one Promise object , The object has one then Method , Allows you to specify callback functions . such as ,f1 Callback function for f2, It can be written. :

f1().then(f2);

Callbacks become chained , The process of the program can be seen clearly , And there's a whole set of matching methods , Many powerful functions can be realized .

such as , Specify multiple callback functions :

f1().then(f2).then(f3);

Another example , Specifies the callback function when an error occurs :

f1().then(f2).fail(f3);

and , It also has a benefit that none of the first three methods have : If a task has been completed , Add callback function again , The callback function executes immediately . therefore , You don't have to worry about missing an event or signal . The drawback of this approach is to write and understand , It's relatively difficult .

javascript Asynchronous programming method of more related articles

  1. Three asynchronous programming methods you must master callbacks,listeners,promise

    Catalog : Preface Callbacks Listeners Promise Preface coder We all know ,javascript The language runtime environment is single threaded , This means that no two lines of code can run at the same time . When multitasking is going on at the same time , In essence ...

  2. javascript Asynchronous programming

    Synchronous and asynchronous Before introducing asynchrony , Take a look back. , Synchronous programming , That is, the computer executes the code line by line in order , The time-consuming execution of current code task will block the execution of subsequent code . Synchronous programming , It's a typical request - Response model , When a request calls a function or method , ...

  3. Javascript Summary of asynchronous programming methods

    Now we have three functions ,f1, f2, f3 In a normal way, we would write code like this : function f1 (){}; function f2 (){}; function f3 (){}; // Call the function here ...

  4. JavaScript Asynchronous programming in

    One About asynchronous events Event is JavaScript One of the most important characteristics ,nodejs Is the use js This is an asynchronous design . So here's the event mechanism . In a js In file , If you want to run a function , Yes 2 Medium means , One is straight ...

  5. say javascript Asynchronous programming

    as everyone knows javascript It's single threaded , It was originally designed for browsers GUI programing language ,GUI One of the features of programming is to guarantee UI Threads must not block , Otherwise, the experience is not good , Even the interface is stuck . The so-called single thread is that only one task can be completed at a time , ...

  6. [JavaScript] Asynchronous programming of a Gernerator Example

    <html> <head> <meta charset="UTF-8"> <title>Generator Demo</tit ...

  7. javascript To achieve asynchronous programming 4 Methods

    1. Callback function . 2. Event monitoring .  Ideas : Use event driven mode . Task execution does not depend on the order of the code , It depends on whether an event happens 3. Observer mode ( Release / A subscription model )    The code is as follows : jQuery.subscribe ...

  8. javascript Collection of asynchronous programming solutions for

    origin Didn't understand js Asynchronous students look at the following example : for (var i = 0; i < 5; i++) { // Simulate an asynchronous operation setTimeout(() => { console.log(i ...

  9. JavaScript Asynchronous programming (2)- The Precursors :jsDeferred

    JavaScript There are many ways to implement asynchronous programming , The most dazzling is ECMAScript 6 Normative Promise object , It comes from CommonJS The group's efforts :Promise/A+ standard . Research javascri ...

Random recommendation

  1. *HDU1907 game

    John Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submis ...

  2. Python——Day3 Knowledge point —— File operations

    One . Open file File handle  = open(' File path ', ' Pattern ') When opening a file , You need to specify the file path and how to open the file , After opening , To get the file handle , Operate on this file in the future through this file handle . The modes for opening files are : r ...

  3. css3 in filter All kinds of special effects

    css3 Medium filter Attributes can be said to be simple to use and powerful , These effects act on the image to achieve some special effects ( It can also act on vidio On , Here's just the picture effects ). Browser compatibility At present, the major browsers for css3 It's very compatible with , newest ...

  4. 【LeetCode OJ】Distinct Subsequences

    Problem Link: http://oj.leetcode.com/problems/distinct-subsequences/ A classic problem using Dynamic ...

  5. .NET The singleton pattern ------- All kinds of writing &amp;&amp; verification

    .NET The singleton pattern ------- All kinds of writing && verification Preface Singleton mode is no stranger to everyone , And it's easy to understand how it works , This article also does not provide the detailed principle analysis of singleton pattern , The purpose of this article is to show that C# All kinds of singleton patterns in ...

  6. rounding PK The bank rounded

    describe In actual development decimal.Round(1.23525,4)!=1.2353 the truth is that 1.2352, and decimal.Round(1.23535,4)==1.2354 explain rounding : When rounding off the number of bits ...

  7. Mysql stored procedure 、 function 、 Permission checking for triggers and views

    When stored procedures . function . After triggers and views are created , It's not just the creator who has to execute , Other users may also need to perform , let me put it another way , The executor may not be the creator himself , So when executing a stored procedure ,MySQL How to do authority check ? By default ,MySQL take ...

  8. utilize Ajax Change the way requests are sent

    Because the test needs simulation Head request , terms of settlement : First visit the site you want to request , Then execute the following code in the browser console , The request mode is the value given by the parameter : var xmlHttp; if (window.ActiveXObjec ...

  9. javascript Sparse arrays in (sparse array) And dense arrays

    Study underscore.js Array correlation API When . Yes sparse array This thing , Never touched . Let's learn what sparse arrays and dense arrays are . What is a dense array ? stay java and C In language , An array is a piece of string ...

  10. hive: The number of dynamic partitions allowed by default is 100, Exception thrown :

    After creating a partition table , Perform dynamic partitioning, insert data , Error thrown : Caused by: org.apache.hadoop.hive.ql.metadata.HiveFatalException: [Erro ...