Skip to content

Instantly share code, notes, and snippets.

@thiagoh
Last active February 3, 2018 00:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thiagoh/87e46855fbe8bf57aaab615cfb93d2a1 to your computer and use it in GitHub Desktop.
Save thiagoh/87e46855fbe8bf57aaab615cfb93d2a1 to your computer and use it in GitHub Desktop.
redux asynchronous actions dispatching one after the other
// oneAction could be setCountry
export const oneAction = params => {
return {
type: "ONE_ACTION",
payload: Promise.resolve(`(value from oneAction ${params})`)
};
};
// otherAction could be fetchRegions
export const otherAction = params => {
return {
type: "OTHER_ACTION",
payload: Promise.resolve(`(value from otherAction ${params})`)
};
};
import React, { Component } from "react";
import { connect } from "react-redux";
import { oneAction, otherAction } from "../actions";
export class AppImpl extends Component {
handleSomething(someValue) {
this.props.oneAction(someValue).then(
oneActionResult => {
console.log("result from oneAction\n", oneActionResult);
return this.props
.otherAction(oneActionResult.payload)
.then(otherActionResult => {
console.log("result from otherAction\n", otherActionResult);
});
},
error => {
// handle error
}
);
}
render() {
return (
<div>
<h3>Asynchronouse Actions dispatch</h3>
<button
onClick={() => this.handleSomething.bind(this)("some useless value")}
>
Click me
</button>
</div>
);
}
}
export const App = connect(
state => ({
state
}),
{ oneAction, otherAction }
)(AppImpl);
result from oneAction
  {type: "ONE_ACTION", payload: "(value from oneAction some useless value)"}
result from otherAction
  {type: "OTHER_ACTION", payload: "(value from otherAction (value from oneAction some useless value))"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment