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

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

等待所有諾言解決

等待所有諾言解決

蝴蝶刀刀 2019-11-12 12:47:48
因此,我遇到了多個未知長度的promise鏈的情況。我希望在處理所有鏈條后執(zhí)行一些操作。那有可能嗎?這是一個例子:app.controller('MainCtrl', function($scope, $q, $timeout) {    var one = $q.defer();    var two = $q.defer();    var three = $q.defer();    var all = $q.all([one.promise, two.promise, three.promise]);    all.then(allSuccess);    function success(data) {        console.log(data);        return data + "Chained";    }    function allSuccess(){        console.log("ALL PROMISES RESOLVED")    }    one.promise.then(success).then(success);    two.promise.then(success);    three.promise.then(success).then(success).then(success);    $timeout(function () {        one.resolve("one done");    }, Math.random() * 1000);    $timeout(function () {        two.resolve("two done");    }, Math.random() * 1000);    $timeout(function () {        three.resolve("three done");    }, Math.random() * 1000);});在此示例中,我設置了$q.all()承諾1,,2和3,這些承諾會在某個隨機時間得到解決。然后,我將諾言添加到第一和第三的結尾。我想all在所有鏈條都解決后解決。這是運行此代碼時的輸出:one done one doneChainedtwo donethree doneALL PROMISES RESOLVEDthree doneChainedthree doneChainedChained 有沒有辦法等待連鎖解決?
查看完整描述

3 回答

?
慕慕森

TA貢獻1856條經(jīng)驗 獲得超17個贊

當所有鏈條都解決后,我希望所有人解決。


當然,然后將每個鏈的承諾傳遞給all()而不是最初的承諾:


$q.all([one.promise, two.promise, three.promise]).then(function() {

    console.log("ALL INITIAL PROMISES RESOLVED");

});


var onechain   = one.promise.then(success).then(success),

    twochain   = two.promise.then(success),

    threechain = three.promise.then(success).then(success).then(success);


$q.all([onechain, twochain, threechain]).then(function() {

    console.log("ALL PROMISES RESOLVED");

});


查看完整回答
反對 回復 2019-11-12
?
慕容森

TA貢獻1853條經(jīng)驗 獲得超18個贊

最近出現(xiàn)了這個問題,但是承諾數(shù)量未知。使用jQuery.map()解決了。


function methodThatChainsPromises(args) {


    //var args = [

    //    'myArg1',

    //    'myArg2',

    //    'myArg3',

    //];


    var deferred = $q.defer();

    var chain = args.map(methodThatTakeArgAndReturnsPromise);


    $q.all(chain)

    .then(function () {

        $log.debug('All promises have been resolved.');

        deferred.resolve();

    })

    .catch(function () {

        $log.debug('One or more promises failed.');

        deferred.reject();

    });


    return deferred.promise;

}


查看完整回答
反對 回復 2019-11-12
  • 3 回答
  • 0 關注
  • 670 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號