第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

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

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

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

3 回答

?
一只名叫tom的貓

TA貢獻(xiàn)1906條經(jīng)驗(yàn) 獲得超3個(gè)贊

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

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

將記錄下列結(jié)果:

abc123undefined

當(dāng)

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

-更新:

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

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

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

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

上面的代碼并行地發(fā)出兩個(gè)http請(qǐng)求,從而使請(qǐng)求更快地完成,而下面的http請(qǐng)求是按順序運(yùn)行的,從而減少了服務(wù)器負(fù)載。

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


查看完整回答
反對(duì) 回復(fù) 2019-06-28
?
幕布斯7119047

TA貢獻(xiàn)1794條經(jīng)驗(yàn) 獲得超8個(gè)贊

.done()只有一個(gè)回調(diào),這就是成功的回調(diào)。

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

.fail()只有一個(gè)失敗回調(diào)

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


查看完整回答
反對(duì) 回復(fù) 2019-06-28
  • 3 回答
  • 0 關(guān)注
  • 684 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)