Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

react redux calling async method and dispatch on loop #1381

Closed
aaeronn opened this issue Feb 10, 2016 · 3 comments
Closed

react redux calling async method and dispatch on loop #1381

aaeronn opened this issue Feb 10, 2016 · 3 comments
Labels

Comments

@aaeronn
Copy link

aaeronn commented Feb 10, 2016

Lets say I am calling a dispatch method in my actions :

return dispatch(add_data(data)).then(function(response){console.log("Done")}

and my add_data method looks like:

export function add_data(data){
  return (dispatch, getState) => {
    for(var i in data){
      var data = {'url': data[i]}
      return dispatch(myApi({url: "some_url", method: "POST", data: data,}
        )).then(response => {
        console.log(response)
        return response.json()
      }).then(response => {
        console.log(response)
        return dispatch({response, type: types.ADD_DATA})
      })
    }
  }
}

It is working for only first data. Only first data is dispatched. It is inside loop and I expect it to dispatch for all the data from loop.

If I dont user return in my dispatch it does not wait for function to complete

How can I achieve this ?

Thank you

@danielepolencic
Copy link

I guess you need to wait for all the promises to be resolved before calling the return. At the moment, once you're in the first iteration of the loop you hit return and immediately get out of the function.

Dispatch actions are supposed to be plain objects, but yours are promises. Is there something I'm missing out?

@aaeronn
Copy link
Author

aaeronn commented Feb 10, 2016

Thank you I will try this out

@aaeronn
Copy link
Author

aaeronn commented Feb 10, 2016

Thank you that was so much helpful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants