Synchronous and asynchronous processing methods in JS

Source: Internet
Author: User

When using an asynchronous request, it is sometimes necessary to return the result of an asynchronous request to another JS function, in which case an asynchronous request returns the result of the request, which is where the JS function has performed the subsequent operation, that is, the return has been executed, which will result in a null character return.

Summary: To handle the results returned by 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 completed and the function has already executed return.
function fn () {

var result = "";

$.ajax ({
URL: ' Your URL ',
Data:{name:value},
Cache:false,
Async:true,
Type: "POST",
Success:function (data) {
Do something ....

result = ....
}

Processing of data returned in Ajax can also be an error

return result;
}
1 Asynchronous Request mode:
$.ajax ({
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
$.ajax ({
URL: ' Your URL ',
Data:{name:value},
Cache:false,
Async:false,
Type: "POST",
DataType: ' json/xml/html ',
Success:function (Result) {
Do something ....
}
});

Synchronous and asynchronous processing methods in JS

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.