Skip to content

Instantly share code, notes, and snippets.

@cqfd
Last active August 29, 2015 14:22
Show Gist options
  • Save cqfd/3a05c79f53dda47d0dcc to your computer and use it in GitHub Desktop.
Save cqfd/3a05c79f53dda47d0dcc to your computer and use it in GitHub Desktop.
Example of using co and React.
const Promise = require('bluebird');
const co = require('co');
const React = require('react');
function* sleep(ms) {
yield Promise.delay(ms);
}
function usersView(users) {
const lis = users.map(u => <li>{u.name}</li>);
return <ul>{lis}</ul>;
}
const UsersComponent = React.createClass({
getInitialState() {
return { users: [] }
},
componentDidMount() {
co(function*() {
while (true) {
const users = yield $.ajax("/home/api");
this.setState({users: users});
yield sleep(2000);
}
}.bind(this)).catch(e => console.log("hmm", e));
},
render() {
return usersView(this.state.users);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment