0

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

raydenl
  • 397
  • 1
  • 4
  • 16
  • 1
    "The answer printed to the console is 'A', not 'B'" --- it is `B` that printed. http://jsfiddle.net/hyjokegu/ Btw, your `chainer` implementation might look like `return methodA().then(methodB);` instead. – zerkms Dec 10 '14 at 00:55
  • Hmmm, so it does... must be something else wrong... – raydenl Dec 10 '14 at 01:17
  • Btw, you should simplify to `methodA().then(methodB)` and `chainer().done(…)` – Bergi Dec 10 '14 at 01:45
  • I have other parameters feeding into methodB, so can't simplify it. – raydenl Dec 10 '14 at 01:54
  • Crazily enough, it still returns the result from methodA. I have simplified it down the the same as I have posted here, and it still returns A... it is doing my head in... even had another guy at work go over it and we compared it with the jsfiddle example and they are identical. – raydenl Dec 10 '14 at 01:55
  • Use `when`js instead. https://github.com/cujojs/when – Andi N. Dirgantara Dec 10 '14 at 03:28

1 Answers1

0

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.

raydenl
  • 397
  • 1
  • 4
  • 16