Async/await redux thunk not returning promise to action correctly












0















I have a thunk using Axios that's posting to an Express route using Sequelize.



The route is posting correctly (ie. data is getting added to the db) but the action inside of the React component isn't behaving as expected. Using async/await, I expect the action to wait until it completes the db post before continuing but that's not the case here. I'm getting undefined from the action.



The thunk hits the express route where I'm dispatching the action to update my redux store and returning the response:



const addedNewList = (newList) => ({type: ADD_NEW_LIST, newList})

export const addNewList = (name, userId) => async dispatch => {
try {
const { data } = await axios.post('/api/list/add', { name, userId })
dispatch(addedNewList(data))
return data
} catch (err) {
console.error(err)
}
}


Using debugger, I can confirm that return data is in fact returning the response from the server that I need. I can also confirm that the redux store is getting updated correctly.



But here, when I try and access that response data as result, I get undefined:



  handleSubmit = async () => {
const result = await this.props.addNewList(this.state.name, this.props.userId)

// ** result is 'undefined' **

this.handleClose()

// pass off the results
}


If I add a setTimeout after I evoke the addNewList action, it works as expected. This suggests to me that maybe it's not returning a promise? But my understanding was that if you returned the response from the server in the thunk, it would do that.



For completeness, here is my route which I've also confirmed with debugger that data is being passed as expected:



const userAuth = function(req, res, next) {
if (req.isAuthenticated()) {
return next()
}
res.status(401).send('Unauthorized user')
}

router.post('/add', userAuth, async (req, res, next) => {
const { name, userId } = req.body
try {
const list = await List.create({ name, userId })
res.json(list)
} catch(err) { next(err) }
})


Why is the action returning undefined in the handleSubmit method?










share|improve this question



























    0















    I have a thunk using Axios that's posting to an Express route using Sequelize.



    The route is posting correctly (ie. data is getting added to the db) but the action inside of the React component isn't behaving as expected. Using async/await, I expect the action to wait until it completes the db post before continuing but that's not the case here. I'm getting undefined from the action.



    The thunk hits the express route where I'm dispatching the action to update my redux store and returning the response:



    const addedNewList = (newList) => ({type: ADD_NEW_LIST, newList})

    export const addNewList = (name, userId) => async dispatch => {
    try {
    const { data } = await axios.post('/api/list/add', { name, userId })
    dispatch(addedNewList(data))
    return data
    } catch (err) {
    console.error(err)
    }
    }


    Using debugger, I can confirm that return data is in fact returning the response from the server that I need. I can also confirm that the redux store is getting updated correctly.



    But here, when I try and access that response data as result, I get undefined:



      handleSubmit = async () => {
    const result = await this.props.addNewList(this.state.name, this.props.userId)

    // ** result is 'undefined' **

    this.handleClose()

    // pass off the results
    }


    If I add a setTimeout after I evoke the addNewList action, it works as expected. This suggests to me that maybe it's not returning a promise? But my understanding was that if you returned the response from the server in the thunk, it would do that.



    For completeness, here is my route which I've also confirmed with debugger that data is being passed as expected:



    const userAuth = function(req, res, next) {
    if (req.isAuthenticated()) {
    return next()
    }
    res.status(401).send('Unauthorized user')
    }

    router.post('/add', userAuth, async (req, res, next) => {
    const { name, userId } = req.body
    try {
    const list = await List.create({ name, userId })
    res.json(list)
    } catch(err) { next(err) }
    })


    Why is the action returning undefined in the handleSubmit method?










    share|improve this question

























      0












      0








      0








      I have a thunk using Axios that's posting to an Express route using Sequelize.



      The route is posting correctly (ie. data is getting added to the db) but the action inside of the React component isn't behaving as expected. Using async/await, I expect the action to wait until it completes the db post before continuing but that's not the case here. I'm getting undefined from the action.



      The thunk hits the express route where I'm dispatching the action to update my redux store and returning the response:



      const addedNewList = (newList) => ({type: ADD_NEW_LIST, newList})

      export const addNewList = (name, userId) => async dispatch => {
      try {
      const { data } = await axios.post('/api/list/add', { name, userId })
      dispatch(addedNewList(data))
      return data
      } catch (err) {
      console.error(err)
      }
      }


      Using debugger, I can confirm that return data is in fact returning the response from the server that I need. I can also confirm that the redux store is getting updated correctly.



      But here, when I try and access that response data as result, I get undefined:



        handleSubmit = async () => {
      const result = await this.props.addNewList(this.state.name, this.props.userId)

      // ** result is 'undefined' **

      this.handleClose()

      // pass off the results
      }


      If I add a setTimeout after I evoke the addNewList action, it works as expected. This suggests to me that maybe it's not returning a promise? But my understanding was that if you returned the response from the server in the thunk, it would do that.



      For completeness, here is my route which I've also confirmed with debugger that data is being passed as expected:



      const userAuth = function(req, res, next) {
      if (req.isAuthenticated()) {
      return next()
      }
      res.status(401).send('Unauthorized user')
      }

      router.post('/add', userAuth, async (req, res, next) => {
      const { name, userId } = req.body
      try {
      const list = await List.create({ name, userId })
      res.json(list)
      } catch(err) { next(err) }
      })


      Why is the action returning undefined in the handleSubmit method?










      share|improve this question














      I have a thunk using Axios that's posting to an Express route using Sequelize.



      The route is posting correctly (ie. data is getting added to the db) but the action inside of the React component isn't behaving as expected. Using async/await, I expect the action to wait until it completes the db post before continuing but that's not the case here. I'm getting undefined from the action.



      The thunk hits the express route where I'm dispatching the action to update my redux store and returning the response:



      const addedNewList = (newList) => ({type: ADD_NEW_LIST, newList})

      export const addNewList = (name, userId) => async dispatch => {
      try {
      const { data } = await axios.post('/api/list/add', { name, userId })
      dispatch(addedNewList(data))
      return data
      } catch (err) {
      console.error(err)
      }
      }


      Using debugger, I can confirm that return data is in fact returning the response from the server that I need. I can also confirm that the redux store is getting updated correctly.



      But here, when I try and access that response data as result, I get undefined:



        handleSubmit = async () => {
      const result = await this.props.addNewList(this.state.name, this.props.userId)

      // ** result is 'undefined' **

      this.handleClose()

      // pass off the results
      }


      If I add a setTimeout after I evoke the addNewList action, it works as expected. This suggests to me that maybe it's not returning a promise? But my understanding was that if you returned the response from the server in the thunk, it would do that.



      For completeness, here is my route which I've also confirmed with debugger that data is being passed as expected:



      const userAuth = function(req, res, next) {
      if (req.isAuthenticated()) {
      return next()
      }
      res.status(401).send('Unauthorized user')
      }

      router.post('/add', userAuth, async (req, res, next) => {
      const { name, userId } = req.body
      try {
      const list = await List.create({ name, userId })
      res.json(list)
      } catch(err) { next(err) }
      })


      Why is the action returning undefined in the handleSubmit method?







      reactjs express redux sequelize.js






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 17:28









      jami0821jami0821

      236112




      236112
























          2 Answers
          2






          active

          oldest

          votes


















          0














          Try returning the dispatch of addedNewList(data) instead:



          export const addNewList = (name, userId) => async dispatch => {
          try {
          const { data } = await axios.post('/api/list/add', { name, userId })
          return Promise.resolve(dispatch(addedNewList(data)));
          } catch (err) {
          console.error(err)
          }
          }


          That being said, you could consider restructuring the component to instead utilize mapStateToProps to use values/result from the updated Redux store rather than explicitly awaiting the response and manually passing the value?






          share|improve this answer
























          • shouldn't you use let and not const for await, thought I read something about const not being ideal with this.

            – Tall Paul
            Nov 15 '18 at 17:44











          • I haven't heard about that, but you if you want to provide an alternative answer explaining why, it could only help get this issue resolved.

            – Alexander Staroselsky
            Nov 15 '18 at 17:45











          • @AlexanderStaroselsky Thanks for your help. I think suggesting to restructure the component is the right direction. What I'm actually doing after the action is called is to get the ID for the newly created list and use it to display an updated url via history.push. But I could easily do this by moving that into the components render and use the redux store for that information when it's ready.

            – jami0821
            Nov 15 '18 at 17:47













          • @AlexanderStaroselsky So it actually does work if I move the next step into render and just use mapStateToProps for the updated state. Your answer is correct in that I really just needed to restructure the component. Wrapping the dispatch with Promise became unnecessary after I did that though. Thanks!

            – jami0821
            Nov 15 '18 at 17:50






          • 1





            @jami0821 Glad to hear you were able to resolve it! You may want to create an answer highlighting what you did specifically as it could help others experiencing something similar.

            – Alexander Staroselsky
            Nov 15 '18 at 17:52





















          0














          The response from Alexander got me on the right track so I'm sharing my solution in case it helps someone (as he suggested).



          While I could have continued to try and solve this by wrapping the dispatch in a Promise, the better solution was to rethink how the component was structured.



          In my situation, I wanted to get the ID for the newly created row in the database so that I could pass it into history.push.



            handleSubmit = async () => {
          const result = await this.props.addNewList(this.state.name, this.props.userId)

          this.handleClose()

          history.push(`/list/${result.id}`)
          }


          With result coming back undefined, the url was not updating correctly.



          The better solution was to access the new data from the redux store where it was updated. This way I could be certain the history wouldn't get updated until the data was ready.



          So my updated component now looked something like this where the history wouldn't update until a newId was available:



            handleSubmit = () => {
          this.props.addNewList(this.state.name, this.props.userId)
          this.handleClose()
          }

          render(){
          const { newId } = this.props

          if (newId) {
          history.push(`/list/${newId}`)
          }

          return (
          ....
          )
          }
          }

          const mapStateToProps = (state) => {
          return {
          newId: state.list.newId
          }
          }


          Instead of putting this into render, I could probably also use a component lifecylcle method like componentWillReceiveProps or similar.






          share|improve this answer

























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53324932%2fasync-await-redux-thunk-not-returning-promise-to-action-correctly%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Try returning the dispatch of addedNewList(data) instead:



            export const addNewList = (name, userId) => async dispatch => {
            try {
            const { data } = await axios.post('/api/list/add', { name, userId })
            return Promise.resolve(dispatch(addedNewList(data)));
            } catch (err) {
            console.error(err)
            }
            }


            That being said, you could consider restructuring the component to instead utilize mapStateToProps to use values/result from the updated Redux store rather than explicitly awaiting the response and manually passing the value?






            share|improve this answer
























            • shouldn't you use let and not const for await, thought I read something about const not being ideal with this.

              – Tall Paul
              Nov 15 '18 at 17:44











            • I haven't heard about that, but you if you want to provide an alternative answer explaining why, it could only help get this issue resolved.

              – Alexander Staroselsky
              Nov 15 '18 at 17:45











            • @AlexanderStaroselsky Thanks for your help. I think suggesting to restructure the component is the right direction. What I'm actually doing after the action is called is to get the ID for the newly created list and use it to display an updated url via history.push. But I could easily do this by moving that into the components render and use the redux store for that information when it's ready.

              – jami0821
              Nov 15 '18 at 17:47













            • @AlexanderStaroselsky So it actually does work if I move the next step into render and just use mapStateToProps for the updated state. Your answer is correct in that I really just needed to restructure the component. Wrapping the dispatch with Promise became unnecessary after I did that though. Thanks!

              – jami0821
              Nov 15 '18 at 17:50






            • 1





              @jami0821 Glad to hear you were able to resolve it! You may want to create an answer highlighting what you did specifically as it could help others experiencing something similar.

              – Alexander Staroselsky
              Nov 15 '18 at 17:52


















            0














            Try returning the dispatch of addedNewList(data) instead:



            export const addNewList = (name, userId) => async dispatch => {
            try {
            const { data } = await axios.post('/api/list/add', { name, userId })
            return Promise.resolve(dispatch(addedNewList(data)));
            } catch (err) {
            console.error(err)
            }
            }


            That being said, you could consider restructuring the component to instead utilize mapStateToProps to use values/result from the updated Redux store rather than explicitly awaiting the response and manually passing the value?






            share|improve this answer
























            • shouldn't you use let and not const for await, thought I read something about const not being ideal with this.

              – Tall Paul
              Nov 15 '18 at 17:44











            • I haven't heard about that, but you if you want to provide an alternative answer explaining why, it could only help get this issue resolved.

              – Alexander Staroselsky
              Nov 15 '18 at 17:45











            • @AlexanderStaroselsky Thanks for your help. I think suggesting to restructure the component is the right direction. What I'm actually doing after the action is called is to get the ID for the newly created list and use it to display an updated url via history.push. But I could easily do this by moving that into the components render and use the redux store for that information when it's ready.

              – jami0821
              Nov 15 '18 at 17:47













            • @AlexanderStaroselsky So it actually does work if I move the next step into render and just use mapStateToProps for the updated state. Your answer is correct in that I really just needed to restructure the component. Wrapping the dispatch with Promise became unnecessary after I did that though. Thanks!

              – jami0821
              Nov 15 '18 at 17:50






            • 1





              @jami0821 Glad to hear you were able to resolve it! You may want to create an answer highlighting what you did specifically as it could help others experiencing something similar.

              – Alexander Staroselsky
              Nov 15 '18 at 17:52
















            0












            0








            0







            Try returning the dispatch of addedNewList(data) instead:



            export const addNewList = (name, userId) => async dispatch => {
            try {
            const { data } = await axios.post('/api/list/add', { name, userId })
            return Promise.resolve(dispatch(addedNewList(data)));
            } catch (err) {
            console.error(err)
            }
            }


            That being said, you could consider restructuring the component to instead utilize mapStateToProps to use values/result from the updated Redux store rather than explicitly awaiting the response and manually passing the value?






            share|improve this answer













            Try returning the dispatch of addedNewList(data) instead:



            export const addNewList = (name, userId) => async dispatch => {
            try {
            const { data } = await axios.post('/api/list/add', { name, userId })
            return Promise.resolve(dispatch(addedNewList(data)));
            } catch (err) {
            console.error(err)
            }
            }


            That being said, you could consider restructuring the component to instead utilize mapStateToProps to use values/result from the updated Redux store rather than explicitly awaiting the response and manually passing the value?







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 15 '18 at 17:33









            Alexander StaroselskyAlexander Staroselsky

            13.6k42342




            13.6k42342













            • shouldn't you use let and not const for await, thought I read something about const not being ideal with this.

              – Tall Paul
              Nov 15 '18 at 17:44











            • I haven't heard about that, but you if you want to provide an alternative answer explaining why, it could only help get this issue resolved.

              – Alexander Staroselsky
              Nov 15 '18 at 17:45











            • @AlexanderStaroselsky Thanks for your help. I think suggesting to restructure the component is the right direction. What I'm actually doing after the action is called is to get the ID for the newly created list and use it to display an updated url via history.push. But I could easily do this by moving that into the components render and use the redux store for that information when it's ready.

              – jami0821
              Nov 15 '18 at 17:47













            • @AlexanderStaroselsky So it actually does work if I move the next step into render and just use mapStateToProps for the updated state. Your answer is correct in that I really just needed to restructure the component. Wrapping the dispatch with Promise became unnecessary after I did that though. Thanks!

              – jami0821
              Nov 15 '18 at 17:50






            • 1





              @jami0821 Glad to hear you were able to resolve it! You may want to create an answer highlighting what you did specifically as it could help others experiencing something similar.

              – Alexander Staroselsky
              Nov 15 '18 at 17:52





















            • shouldn't you use let and not const for await, thought I read something about const not being ideal with this.

              – Tall Paul
              Nov 15 '18 at 17:44











            • I haven't heard about that, but you if you want to provide an alternative answer explaining why, it could only help get this issue resolved.

              – Alexander Staroselsky
              Nov 15 '18 at 17:45











            • @AlexanderStaroselsky Thanks for your help. I think suggesting to restructure the component is the right direction. What I'm actually doing after the action is called is to get the ID for the newly created list and use it to display an updated url via history.push. But I could easily do this by moving that into the components render and use the redux store for that information when it's ready.

              – jami0821
              Nov 15 '18 at 17:47













            • @AlexanderStaroselsky So it actually does work if I move the next step into render and just use mapStateToProps for the updated state. Your answer is correct in that I really just needed to restructure the component. Wrapping the dispatch with Promise became unnecessary after I did that though. Thanks!

              – jami0821
              Nov 15 '18 at 17:50






            • 1





              @jami0821 Glad to hear you were able to resolve it! You may want to create an answer highlighting what you did specifically as it could help others experiencing something similar.

              – Alexander Staroselsky
              Nov 15 '18 at 17:52



















            shouldn't you use let and not const for await, thought I read something about const not being ideal with this.

            – Tall Paul
            Nov 15 '18 at 17:44





            shouldn't you use let and not const for await, thought I read something about const not being ideal with this.

            – Tall Paul
            Nov 15 '18 at 17:44













            I haven't heard about that, but you if you want to provide an alternative answer explaining why, it could only help get this issue resolved.

            – Alexander Staroselsky
            Nov 15 '18 at 17:45





            I haven't heard about that, but you if you want to provide an alternative answer explaining why, it could only help get this issue resolved.

            – Alexander Staroselsky
            Nov 15 '18 at 17:45













            @AlexanderStaroselsky Thanks for your help. I think suggesting to restructure the component is the right direction. What I'm actually doing after the action is called is to get the ID for the newly created list and use it to display an updated url via history.push. But I could easily do this by moving that into the components render and use the redux store for that information when it's ready.

            – jami0821
            Nov 15 '18 at 17:47







            @AlexanderStaroselsky Thanks for your help. I think suggesting to restructure the component is the right direction. What I'm actually doing after the action is called is to get the ID for the newly created list and use it to display an updated url via history.push. But I could easily do this by moving that into the components render and use the redux store for that information when it's ready.

            – jami0821
            Nov 15 '18 at 17:47















            @AlexanderStaroselsky So it actually does work if I move the next step into render and just use mapStateToProps for the updated state. Your answer is correct in that I really just needed to restructure the component. Wrapping the dispatch with Promise became unnecessary after I did that though. Thanks!

            – jami0821
            Nov 15 '18 at 17:50





            @AlexanderStaroselsky So it actually does work if I move the next step into render and just use mapStateToProps for the updated state. Your answer is correct in that I really just needed to restructure the component. Wrapping the dispatch with Promise became unnecessary after I did that though. Thanks!

            – jami0821
            Nov 15 '18 at 17:50




            1




            1





            @jami0821 Glad to hear you were able to resolve it! You may want to create an answer highlighting what you did specifically as it could help others experiencing something similar.

            – Alexander Staroselsky
            Nov 15 '18 at 17:52







            @jami0821 Glad to hear you were able to resolve it! You may want to create an answer highlighting what you did specifically as it could help others experiencing something similar.

            – Alexander Staroselsky
            Nov 15 '18 at 17:52















            0














            The response from Alexander got me on the right track so I'm sharing my solution in case it helps someone (as he suggested).



            While I could have continued to try and solve this by wrapping the dispatch in a Promise, the better solution was to rethink how the component was structured.



            In my situation, I wanted to get the ID for the newly created row in the database so that I could pass it into history.push.



              handleSubmit = async () => {
            const result = await this.props.addNewList(this.state.name, this.props.userId)

            this.handleClose()

            history.push(`/list/${result.id}`)
            }


            With result coming back undefined, the url was not updating correctly.



            The better solution was to access the new data from the redux store where it was updated. This way I could be certain the history wouldn't get updated until the data was ready.



            So my updated component now looked something like this where the history wouldn't update until a newId was available:



              handleSubmit = () => {
            this.props.addNewList(this.state.name, this.props.userId)
            this.handleClose()
            }

            render(){
            const { newId } = this.props

            if (newId) {
            history.push(`/list/${newId}`)
            }

            return (
            ....
            )
            }
            }

            const mapStateToProps = (state) => {
            return {
            newId: state.list.newId
            }
            }


            Instead of putting this into render, I could probably also use a component lifecylcle method like componentWillReceiveProps or similar.






            share|improve this answer






























              0














              The response from Alexander got me on the right track so I'm sharing my solution in case it helps someone (as he suggested).



              While I could have continued to try and solve this by wrapping the dispatch in a Promise, the better solution was to rethink how the component was structured.



              In my situation, I wanted to get the ID for the newly created row in the database so that I could pass it into history.push.



                handleSubmit = async () => {
              const result = await this.props.addNewList(this.state.name, this.props.userId)

              this.handleClose()

              history.push(`/list/${result.id}`)
              }


              With result coming back undefined, the url was not updating correctly.



              The better solution was to access the new data from the redux store where it was updated. This way I could be certain the history wouldn't get updated until the data was ready.



              So my updated component now looked something like this where the history wouldn't update until a newId was available:



                handleSubmit = () => {
              this.props.addNewList(this.state.name, this.props.userId)
              this.handleClose()
              }

              render(){
              const { newId } = this.props

              if (newId) {
              history.push(`/list/${newId}`)
              }

              return (
              ....
              )
              }
              }

              const mapStateToProps = (state) => {
              return {
              newId: state.list.newId
              }
              }


              Instead of putting this into render, I could probably also use a component lifecylcle method like componentWillReceiveProps or similar.






              share|improve this answer




























                0












                0








                0







                The response from Alexander got me on the right track so I'm sharing my solution in case it helps someone (as he suggested).



                While I could have continued to try and solve this by wrapping the dispatch in a Promise, the better solution was to rethink how the component was structured.



                In my situation, I wanted to get the ID for the newly created row in the database so that I could pass it into history.push.



                  handleSubmit = async () => {
                const result = await this.props.addNewList(this.state.name, this.props.userId)

                this.handleClose()

                history.push(`/list/${result.id}`)
                }


                With result coming back undefined, the url was not updating correctly.



                The better solution was to access the new data from the redux store where it was updated. This way I could be certain the history wouldn't get updated until the data was ready.



                So my updated component now looked something like this where the history wouldn't update until a newId was available:



                  handleSubmit = () => {
                this.props.addNewList(this.state.name, this.props.userId)
                this.handleClose()
                }

                render(){
                const { newId } = this.props

                if (newId) {
                history.push(`/list/${newId}`)
                }

                return (
                ....
                )
                }
                }

                const mapStateToProps = (state) => {
                return {
                newId: state.list.newId
                }
                }


                Instead of putting this into render, I could probably also use a component lifecylcle method like componentWillReceiveProps or similar.






                share|improve this answer















                The response from Alexander got me on the right track so I'm sharing my solution in case it helps someone (as he suggested).



                While I could have continued to try and solve this by wrapping the dispatch in a Promise, the better solution was to rethink how the component was structured.



                In my situation, I wanted to get the ID for the newly created row in the database so that I could pass it into history.push.



                  handleSubmit = async () => {
                const result = await this.props.addNewList(this.state.name, this.props.userId)

                this.handleClose()

                history.push(`/list/${result.id}`)
                }


                With result coming back undefined, the url was not updating correctly.



                The better solution was to access the new data from the redux store where it was updated. This way I could be certain the history wouldn't get updated until the data was ready.



                So my updated component now looked something like this where the history wouldn't update until a newId was available:



                  handleSubmit = () => {
                this.props.addNewList(this.state.name, this.props.userId)
                this.handleClose()
                }

                render(){
                const { newId } = this.props

                if (newId) {
                history.push(`/list/${newId}`)
                }

                return (
                ....
                )
                }
                }

                const mapStateToProps = (state) => {
                return {
                newId: state.list.newId
                }
                }


                Instead of putting this into render, I could probably also use a component lifecylcle method like componentWillReceiveProps or similar.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 15 '18 at 18:52

























                answered Nov 15 '18 at 18:06









                jami0821jami0821

                236112




                236112






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53324932%2fasync-await-redux-thunk-not-returning-promise-to-action-correctly%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Florida Star v. B. J. F.

                    Danny Elfman

                    Lugert, Oklahoma