async/await

nanachi可自由使用async/await语法

  1. import React from '@react';
  2. class P extends React.Component {
  3. constructor(){
  4. super();
  5. this.state = {
  6. status: ''
  7. };
  8. this.tapHander = this.tapHander.bind(this);
  9. }
  10. say(){
  11. return new Promise((resolve)=>{
  12. setTimeout(()=>{
  13. resolve('hello nanachi');
  14. }, 2000);
  15. });
  16. }
  17. async tapHander(){
  18. this.setState({status: 'waiting...' });
  19. let result = await this.say();
  20. this.setState({
  21. status: result
  22. });
  23. }
  24. render() {
  25. return (
  26. <div>
  27. <div>status: {this.state.status}</div>
  28. <button onTap={this.tapHander}>click me</button>
  29. </div>
  30. );
  31. }
  32. }
  33. export default P;