StephenGrider / ReduxSimpleStarter

Starter pack for an awesome Udemy course

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Why does chaining a promise after an axios POST still return the original promise's value, when the Promise docs say that the last value should be returned?

vladiibine opened this issue · comments

I'm watching this course (for reference only)
https://www.udemy.com/react-redux/learn/v4/t/lecture/6946622?start=0

I'm at episode number 143, the start time is 6:15.

Initially you have this line which makes a POST request

const request = axios.post(`${ROOT_URL}/posts${API_KEY}`, values)

... then you're chaining a promise, like so

const request = axios.post(`${ROOT_URL}/posts${API_KEY}`, values)
     .then(()=>callback());

The callback is defined inline in a call (to method createPost) like so

class PostsNew extends Component {
   // ...

    onSubmit(values){
        this.props.createPost(values, () => {
            this.props.history.push('/');
        });
    }
}

So reading this about promises https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
(and I'm not a guru in javascript, lemme tell you that) it says that the return value from a promise call like this var promise = some_call().then(()=>{return 3}).then(()=>{return 1})....then(()=>{return 123}), should be the value returned by the last promise in the chain.

However in your example, the value returned by the first promise is used.

I even try to do weird things in the action creators, like this:

const request = axios.post(`${ROOT_URL}/posts${API_KEY}`, values)
    // dunno why the result of axios.post remains the result of the HTTP call, and the .then part()
        .then(()=>{console.log('fdfadsfas inside the freaking callback')})
        .then(()=>4)
        .then(()=>callback());

... and STILL it works, and I get back the expected data.

Am I not seeing something?

Thanks :)

I was thinking that maybe it's possible that I actually DO get my weird data inside the reducer, but because of the redirect, the reducer is called again with the proper state of the application, so I never see the error... lemme investigate that actually. 😄