jQuery deferreds and promises - .then() vs .done()

后端 未结 10 1331
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 06:16

I\'ve been reading about jQuery deferreds and promises and I can\'t see the difference between using .then() & .done() for successful callbacks

10条回答
  •  粉色の甜心
    2020-11-22 07:07

    There is also difference in way that return results are processed (its called chaining, done doesn't chain while then produces call chains)

    promise.then(function (x) { // Suppose promise returns "abc"
        console.log(x);
        return 123;
    }).then(function (x){
        console.log(x);
    }).then(function (x){
        console.log(x)
    })
    

    The following results will get logged:

    abc
    123
    undefined
    

    While

    promise.done(function (x) { // Suppose promise returns "abc"
        console.log(x);
        return 123;
    }).done(function (x){
        console.log(x);
    }).done(function (x){
        console.log(x)
    })
    

    will get the following:

    abc
    abc
    abc
    

    ---------- Update:

    Btw. I forgot to mention, if you return a Promise instead of atomic type value, the outer promise will wait until inner promise resolves:

    promise.then(function (x) { // Suppose promise returns "abc"
        console.log(x);
        return $http.get('/some/data').then(function (result) {
            console.log(result); // suppose result === "xyz"
            return result;
        });
    }).then(function (result){
        console.log(result); // result === xyz
    }).then(function (und){
        console.log(und) // und === undefined, because of absence of return statement in above then
    })
    

    in this way it becomes very straightforward to compose parallel or sequential asynchronous operations such as:

    // Parallel http requests
    promise.then(function (x) { // Suppose promise returns "abc"
        console.log(x);
    
        var promise1 = $http.get('/some/data?value=xyz').then(function (result) {
            console.log(result); // suppose result === "xyz"
            return result;
        });
    
        var promise2 = $http.get('/some/data?value=uvm').then(function (result) {
            console.log(result); // suppose result === "uvm"
            return result;
        });
    
        return promise1.then(function (result1) {
            return promise2.then(function (result2) {
               return { result1: result1, result2: result2; }
            });
        });
    }).then(function (result){
        console.log(result); // result === { result1: 'xyz', result2: 'uvm' }
    }).then(function (und){
        console.log(und) // und === undefined, because of absence of return statement in above then
    })
    

    The above code issues two http requests in parallel thus making the requests complete sooner, while below those http requests are being run sequentially thus reducing server load

    // Sequential http requests
    promise.then(function (x) { // Suppose promise returns "abc"
        console.log(x);
    
        return $http.get('/some/data?value=xyz').then(function (result1) {
            console.log(result1); // suppose result1 === "xyz"
            return $http.get('/some/data?value=uvm').then(function (result2) {
                console.log(result2); // suppose result2 === "uvm"
                return { result1: result1, result2: result2; };
            });
        });
    }).then(function (result){
        console.log(result); // result === { result1: 'xyz', result2: 'uvm' }
    }).then(function (und){
        console.log(und) // und === undefined, because of absence of return statement in above then
    })
    

提交回复
热议问题