The method and difference summary of synchronous and asynchronous processing in JS

Source: Internet
Author: User
Tags json

  To process the results of sending requests after using AJAX requests, it is best to use synchronization requests, here is a brief introduction to the next JS synchronous and asynchronous processing methods and differences, interested friends do not miss

When using an asynchronous request, sometimes it is necessary to return the result of the asynchronous request to another JS function, in which case there will be an asynchronous request return request result, the request is in the JS function has performed the subsequent operation, that is, has been executed, which will cause the return result is null character.     Summary: To process the results of a send request after using an AJAX request, it is best to use a synchronization request.     For example: The following example shows an incorrect return result because the Ajax asynchronous request has not been executed and the function has been returned,    code is as follows: the function fn () {    var result = "";    $.ajax ({  url: ' Your URL ',  data:{name:value},  cache:false,  async:tr ue,  type: "POST",  success:function (data) {  do something....    result = .... }   //processing of data returned in Ajax, error     return result; }    1 Asynchronous request mode:    code is as follows: $.ajax ({&N Bsp URL: ' Your URL ',  data:{name:value},  cache:false,  async:true,  type: ' POST ',  dataType: ' Json/xml/html ',  success:function (Result) {  do something.... } });    2 Synchronous request mode   generation Code as follows: $.ajax ({  url: ' Your URL ',  data:{name:value},  cache:false,  async:false,  type: "POST",  dataType: ' json/xml/html ',  success:function (Result) {  do something.... } }) ;   
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.