Differences between Deferred and promise in jQuery _ jquery

Source: Internet
Author: User
This article mainly introduces the differences between Deferred and promise in jQuery. For more information, see what is the difference between Deferred and Promise?

Promise

A promise is an object returned by an asynchronous function. When you want to write such a function, you need to use a deferred.

var promise = $.ajax({url: "/myServerScript"});promise.done(mySuccessFunction); promise.fail(myErrorFunction); var promise = $.ajax({url: "/myServerScript"}); promise.then(mySuccessFunction,myErrorFunction); 

Promises has the following advantages:

You can call the done () and fail () functions multiple times and use different callback functions. Maybe one of your callback functions is used to stop the animation, one is used to initiate a new AJAX request, and the other is used to display the received data to the user.

var promise = $.ajax({ url: "/myServerScript" });promise.done(myStopAnimationFunction); promise.done(myOtherAjaxFunction); promise.done(myShowInfoFunction); promise.fail(myErrorFunction);

Even after AJAX is called, you can call the done () and fail () functions, and the callback function can be executed immediately. Variable confusion does not occur between different States. When an AJAX call ends, it remains in the success or failure state, which will not change.

You can merge promises. Sometimes you need to make two AJAX requests at the same time and want to call a function when both AJAX requests are successful. To complete this task, you need to use a new $. when () function:

Var promise1 = $. ajax ("/myServerScript1"); var promise2 = $. ajax ("/myServerScript2"); $. when (promise1, promise2 ). done (function (xhrObject1, xhrObject2) {// process two XHR objects });

Deferred

Put simply, the deferred object is jQuery's callback function solution. In English, defer means "delay", so the deferred object means "delay" to a certain point in the future.

What a deferred object can do is similar to a promise object, but it has two functions to trigger the done () and fail () functions.

A deferred object has a resolve () function to process a successful result and execute a function related to done. The reject () function is used to process failed results and execute functions related to fail.

You can provide parameters for both the resolve () and reject () functions, and then they will be passed to the callback functions related to done () and fail.

Summary

JQuery's ajax is to return a promise object, which contains the done (), fail () method; deferred is the process of returning this promise object.

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.