0

Below is my code. The first console message shows result, the second one doesn't... Doesn't this magically break promises... how is this possible?

    this.saveAsTemplate = function(name, asNew) {
        return _saveSet(name, asNew)
            .then(function(result) {
                // -> result is set correctly
                console.log('oh but youll ****ing work... wth?', result);
                $.ajax({
                    url: '/rest/filter/template/'+result.id,
                    type: 'PUT',
                }).success(function(result) {
                    console.log('successfully saved template, result: ', result);
                });
            })
            .then(function(result) {
                // -> result is undefined :(
                console.log('no ****ing result: ', result);
            });

For the last few months I've been having to write additional deferreds just to get around this issue... it's really messing with my code. Help would be greatly appreciated!

EDIT: A good clear example solution of chaining promises properly can be found in this question: (notice everything function returns) How do I chain three asynchronous calls using jQuery promises?

Community
  • 1
  • 1
Sophie McCarrell
  • 2,831
  • 8
  • 31
  • 64
  • you should replace `.success` with `.done`, `.success` is deprecated. (not to be confused with `success:fn`) – Kevin B Oct 17 '14 at 20:17
  • *Your* code is broken. Why do people assume that the problem is always with the external library written by a huge community of professionals who know what their doing, instead of with their own buggy code that has only ever had *one person* look at it? No, you haven't found a bug in jQuery. You've *written* a bug in your own code. – user229044 Oct 17 '14 at 20:21
  • Whoa, i don't see anything in his question that suggests he's blaming the external library. – Kevin B Oct 17 '14 at 20:34
  • ya easy Meager, I don't doubt I'm misunderstanding how jquery ajax promises work. I'm looking for someone to help me understand what I'm doing wrong. Kevin, thanks for the heads up (although the code works now, i imagine itll break in future releases, so cheers for solving a bug that was likely going to show up in 2015 =P). – Sophie McCarrell Oct 17 '14 at 20:38

1 Answers1

4

For .then to chain in that way, you have to return a promise to it.

return $.ajax({

full sample:

this.saveAsTemplate = function(name, asNew) {
    return _saveSet(name, asNew).then(function(result) {
        console.log('oh but youll ****ing work... wth?', result);
        // *** The following line was modified ***
        return $.ajax({
            url: '/rest/filter/template/'+result.id,
            type: 'PUT',
        }).done/*success*/(function(result) {
            console.log('successfully saved template, result: ', result);
        });
    }).then(function(result) {
        console.log('no ****ing result: ', result);
    });
Kevin B
  • 94,570
  • 16
  • 163
  • 180
  • I should be a little clearer with my code. See the function definitely returns a promise, because the first then works. I'll edit my code in my question to be clearer. – Sophie McCarrell Oct 17 '14 at 20:35
  • 1
    uhm... the one in your question does not return a promise, the modification i made corrects that issue. – Kevin B Oct 17 '14 at 20:36
  • 1
    In your first callback passed to `.then()`, you didn't return a promise. In my code, i AM returning a promise. that's the difference. Added comment for clarity. – Kevin B Oct 17 '14 at 20:38
  • oh man, so what you return in the first then, gets given as data to the second then? – Sophie McCarrell Oct 17 '14 at 20:40
  • 1
    Correct. You could, for example, `return "Hello World"` and that's what you'll get in the next then. – Kevin B Oct 17 '14 at 20:40
  • wow. I can't believe I didn't know this. is this how $q works as well? Btw thanks a bajillion. You're awesome! – Sophie McCarrell Oct 17 '14 at 20:42
  • Man, I wonder if I forgot or if I've been writing promises incorrectly for the last year. Well, it's good that I know how this works now. I'll be more intelligent about chaining from now on. – Sophie McCarrell Oct 17 '14 at 20:45