亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

jQuery延遲和承諾-.Then()vs.Done()

jQuery延遲和承諾-.Then()vs.Done()

開心每一天1111 2019-06-28 15:25:37
jQuery延遲和承諾-.Then()vs.Done()我一直在閱讀關于jQuery延遲和承諾的文章,我看不出使用.then() & .done()為了成功的回調。我知道呀艾瑞克·海因茲提到.done()和.success()映射到相同的功能,但我猜也是如此.then()因為所有回調都是在一個成功的操作完成后調用的。有人能告訴我正確的用法嗎?
查看完整描述

3 回答

?
一只名叫tom的貓

TA貢獻1906條經驗 獲得超3個贊

處理返回結果的方式也有差異(稱為鏈接,done不鎖鏈then生產呼叫鏈)

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)})

將記錄下列結果:

abc123undefined

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)})

將得到以下信息:

abc
abc
abc

-更新:

順便說一下。我忘了提到,如果您返回一個承諾而不是原子類型的值,外部承諾將等待內部承諾解決:

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})

通過這種方式,組成并行或順序異步操作變得非常簡單,例如:

// Parallel http requestspromise.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})

上面的代碼并行地發出兩個http請求,從而使請求更快地完成,而下面的http請求是按順序運行的,從而減少了服務器負載。

// Sequential http requestspromise.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})


查看完整回答
反對 回復 2019-06-28
?
幕布斯7119047

TA貢獻1794條經驗 獲得超8個贊

.done()只有一個回調,這就是成功的回調。

.then()有成功的也有失敗的回調

.fail()只有一個失敗回調

所以你該怎么做.你在乎它是成功還是失???


查看完整回答
反對 回復 2019-06-28
  • 3 回答
  • 0 關注
  • 695 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號