Search code examples
javascriptjqueryajaxpromisechain

Two ways of chaining ajax jquery calls. Right one?


I have the next chain of ajax calls:

ajaxCall1().then(function(){
    ajaxCall2(). then(function(){
       ajaxCall3().then(function(){
            ....
        })
    })
 })

As you can see I am not returning the ajax calls but its just work fine. The next way is what I have seen as examples:

 ajaxCall1()
   .then(function(){
        return ajaxCall2()
    })
   .then(function(){
        return ajaxCall3()
    }).then(function(){
        ....
    })

What would be the right way?

Thanks


Solution

  • The first pattern is actually a well-known promise anti-pattern.

    The chainability of promises allows us to get away from nesting callbacks and maintain a vertical structure.