Help with Twich: chain 3 getJson using promis (then)


#1

I’m still struggling to understand how to chain 3 asynchronous calls using promis and then.

I’ve created 3 getJson call functions: user, logo and streamer. All three of them have wrapper functions callUser, callLogo and callStreamer to enable chaining promises from asynchronous calls.

I’m getting the error: “pen.js:105 Uncaught TypeError: Cannot read property ‘then’ of undefined” after the callLogo.

Here is my pen:

Twitch TV


#2

Sorry this is so late, it seems your post got buried.

If you provide a callback to any jQuery AJAX function, you won’t get a promise from it. You should be handling all of your data in a then call.

$.getJSON(url)
    .then(function(data) {
        return $.getJSON(data.url);
    })
    .then(function(dataFromSecondCall) {
       return $.getJSON(dataFromSecondCall.url);
    })
    .then(function(finalData) {
       $("#some-div").html(finalData);
    })
    .catch(function(error) {
        //explode
    });

#3

Thank you! It helped me finished then promises. My twitch project is finally done.
Twitch TV project