Home > Web Front-end > JS Tutorial > The difference between done and then

The difference between done and then

php中世界最好的语言
Release: 2018-03-15 15:12:45
Original
6597 people have browsed it

This time I will bring you the difference between done and then. What are the precautions when using done and then? The following is a practical case, let’s take a look.

jquery's deferred object's done method and then method can implement chain calls, but their functions are different. If you pass in the then method If the method has a return value, it will be passed to the next method called in the chain. The done method is the opposite. Even if the method you pass has a return value, the done method will not pass your return value to the next method called in the chain.

Without further ado, let’s go directly to the example:

var defer = jQuery.Deferred();
defer.done(function(a,b){
  console.log("a = " + a+"b = " + b);
  return a * b;
}).done(function( result ) {
  console.log("result = " + result);
}).then(function( a, b ) {
  console.log("a = " + a+"b = " + b);
  return a * b;
}).done(function( result ) {
      console.log("result = " + result);
}).then(function( a, b ) {
  console.log("a = " + a+"b = " + b);
  return a * b;
}).done(function( result ) {
  console.log("result = " + result);
});
defer.resolve( 2, 3 );
Copy after login

The output result is as follows:

Result analysis:

1. Both the first done and the second done returned defer.resolve(2, 3)

2. The return value of callback in done will not be passed

3. The second done has only one parameter and receives the first parameter 2 of defer.resolve(2, 3), so the result is 2

4. The first then receives defer.resolve(2, 3), receives two parameters, the result is 6, and creates a new deferred object, passing the result to the deferred object

5. The third done received the new deferred object and the passed result, printed the result as 6, and passed the new deferred object to the second then

6. The second A then now receives a new deferred object. It has only one parameter, which is result, so parameter b is not defined. The returned result is NaN. At the same time, a new deferred object is created.

7. The fourth done receives a new one. deferred object, the passed parameter is NaN, and the printed result is naturally NaN

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

How jQuery’s Validate plug-in verifies the input value

Usage of jQuery EasyUI folding panel

The above is the detailed content of The difference between done and then. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template