Skip to content

Instantly share code, notes, and snippets.

@sagar-gavhane
Created November 24, 2018 11:42
Show Gist options
  • Save sagar-gavhane/6d7cf3b2e189a7c54cf6ec3f5049b707 to your computer and use it in GitHub Desktop.
Save sagar-gavhane/6d7cf3b2e189a7c54cf6ec3f5049b707 to your computer and use it in GitHub Desktop.
sequentially execute states.
import React from "react";
import { render } from "react-dom";
import "./styles.scss";
class App extends React.Component {
constructor(props) {
super(props);
this.state = { counter: 1 };
}
render() {
return (
<div>
<button
onClick={async () => {
const seqExecuteStates = counter => {
new Promise(resolve => {
this.setState(
() => {
return { counter };
},
() => {
resolve();
}
);
});
};
await seqExecuteStates(2);
console.log("counter", this.state.counter);
await seqExecuteStates(3);
console.log("counter", this.state.counter);
await seqExecuteStates(4);
console.log("counter", this.state.counter);
}}
>
{this.state.counter}
</button>
</div>
);
}
}
render(<App />, document.getElementById("app"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment