Correct way to join promises together

43 views Asked by At

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?

1

There are 1 answers

0
Kingpin2k On BEST ANSWER

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){
        ...
 });