Search code examples
ember.jsrsvp.js

Correct way to join promises together


Currently I am doing this which is awful:

        //start the initial request
        request({
            ...
        }).then(function(response1) {

            //Start a second request once the first has finished as it is dependent on the original request finishing
            request({
                ...
            }).then(function(response2) {
                ...
            }).catch(function(reason) {
                ...
            });

        }).catch(function(reason){
            ...
        });

How can I avoid the embedded request and include it in the original flow and avoid nesting?


Solution

  • return a promise to the then statement, and it'll wait and use that in the next chained then.

     request({
            ...
     }).then(function(response1) {
       return request({
                ...
       });
     }).then(function(response2){
    
     }).catch(function(reason){
            ...
     });