0

We have a chained promise framework type thing set up. It does an async call, a .then() which processes the response then resolves or rejects the promise, another component that adds a .then() to the promise, and finally this is utilized inside individual components.

This scenario makes no sense to me:

First then calls reject on the promise.

Second then calls its reject callback.

Third then calls its resolve callback.

I would think the third would call its reject callback as well. The goal is that if resolve is called all thens utilize their individual resolve callbacks, if it's rejected each then uses its reject callback.

TheAJAXCall(){
    return new Promise(function(resolve, reject){
        axios({
            //doesn't matter what is here, just pretend it works
        }).then(function(response){
            reject(response)
        });
    });
}

SetupOurCall(){
    return TheAJAXCall().then(function(){
        console.log("second then resolved"); //ignored as expected
        return response;
    }, function(){
        console.log("second then rejected"); //called as expected
    })
}

MyFunction = function(){
    SetupOurCall().then(function(){
        console.log("How did I get resolved");  //this is resolved?!?!?
    }, function(){
        console.log("I want this to be rejected"); //why not rejected?
    })
}
Randy Hall
  • 7,716
  • 16
  • 73
  • 151
  • Remove the second callback to the .then from SetupOurCall, then think about what the implications of that fixing it are. – Kevin B Aug 01 '19 at 20:24
  • @KevinB That doesn't fix it... MyFunction is still logging the resolve callback, SetupOurCall doesn't log anything. Another hint maybe? – Randy Hall Aug 01 '19 at 20:28
  • Avoid the [`Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it)! – Bergi Aug 01 '19 at 20:29
  • 1
    The `function(){ console.log("second then rejected"); //called as expected }` does *handle* the exception. The promise returned by `SetupOurCall()` will be resolved with the `then` called callback's return value - `undefined`. To reject that promise, re-`throw` an error from the handler, or return a rejected promise. – Bergi Aug 01 '19 at 20:31
  • See this for example: https://jsfiddle.net/rsjkn1gb/ you'll note that doThree properly calls reject, but because it has a second .then() callback that doesn't return a rejected promise or an error or throw an error, the final promise is resolved. Omitting the reject handler in doThree would cause the final reject to occur. – Kevin B Aug 01 '19 at 20:32

0 Answers0