简体   繁体   中英

Using Chained Promises with .when

I have 2 methods which return promises (shortened with non-async resolves)

function methodA () {
    var d = $.Deferred();
    d.resolve('A');
    return d.promise();
}

function methodB (dependency) {
    var d = $.Deferred();
    // dependency would be used here
    d.resolve('B');
    return d.promise();
}

And then I have another method which chains these

function chainer () {
    return methodA().then(function(result) {
        return methodB(result);
    });
}

And then I have another method which calls .when on this chainer

function main () {
    $.when(chainer()).done(function (answer) {
        console.log(answer);
    });
}

The answer printed to the console is 'A', not 'B' as I would have expected! Why is this? And how can I get the result of methodB, since this method is dependant on methodA.

Thanks R

Found the issue was due to a bug in jQuery. We use version 1.7.2, and the .then implementation has a bug. You need to still use the deprecated .pipe method. In later versions both work as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM