Skip to content

Instantly share code, notes, and snippets.

View PabloRegen's full-sized avatar
🏠
Working from home

Pablo Regen PabloRegen

🏠
Working from home
  • New York City
View GitHub Profile
// Sync callback
function greetings(callback) {
callback();
}
greetings(() => { console.log('Hi'); });
moreWork(); // will run after console.log
// Async callback
const fs = require('fs');
fs.readFile('/file.md', function callback(err, data) { // fs.readFile is an async method provided by Node
// Blocking
const fs = require('fs');
const data = fs.readFileSync('/file.md'); // blocks here until file is read
console.log(data);
moreWork(); // will run after console.log
// Non-blocking
const fs = require('fs');
fs.readFile('/file.md', (err, data) => {
if (err) throw err;
// Synchronous: 1,2,3
alert(1);
alert(2);
alert(3);
// Asynchronous: 1,3,2
alert(1);
setTimeout(() => alert(2), 0);
alert(3);
@PabloRegen
PabloRegen / render.js
Last active March 4, 2019 10:56
The render method
class App extends Component {
// All previous methods ...
render() {
const { boardStatus, isGameRunning, generation, speed } = this.state;
return (
<div>
<h1>Game of Life</h1>
<BoardGrid boardStatus={boardStatus} onToggleCellStatus={this.handleToggleCellStatus} />
class App extends Component {
state = {...};
runStopButton = () => {...}
handleClearBoard = () => {...}
handleNewBoard = () => {...}
handleToggleCellStatus = () => {...}
handleStep = () => {...}
handleSpeedChange = newSpeed => {
this.setState({ speed: newSpeed });
@PabloRegen
PabloRegen / handleStep.js
Last active March 5, 2019 00:05
Generate next game iteration
class App extends Component {
state = {...};
runStopButton = () => {...}
handleClearBoard = () => {...}
handleNewBoard = () => {...}
handleToggleCellStatus = () => {...}
handleStep = () => {
const nextStep = prevState => {
const boardStatus = prevState.boardStatus;
@PabloRegen
PabloRegen / handleToggleCellStatus.js
Last active March 5, 2019 00:02
Toggle an individual cell status
class App extends Component {
state = {...};
runStopButton = () => {...}
handleClearBoard = () => {...}
handleNewBoard = () => {...}
handleToggleCellStatus = (r,c) => {
const toggleBoardStatus = prevState => {
const clonedBoardStatus = JSON.parse(JSON.stringify(prevState.boardStatus));
clonedBoardStatus[r][c] = !clonedBoardStatus[r][c];
@PabloRegen
PabloRegen / handleClearBoard.js
Last active March 5, 2019 00:00
Clear board and new board methods
class App extends Component {
state = {...};
runStopButton = () => {...}
handleClearBoard = () => {
this.setState({
boardStatus: newBoardStatus(() => false),
generation: 0
});
}
@PabloRegen
PabloRegen / runStopButton.js
Last active March 4, 2019 23:55
Start/Stop button
class App extends Component {
state = {...};
runStopButton = () => {
return this.state.isGameRunning ?
<button type='button' onClick={this.handleStop}>Stop</button> :
<button type='button' onClick={this.handleRun}>Start</button>;
}
// Other methods ...
@PabloRegen
PabloRegen / state.js
Last active March 4, 2019 07:42
Starting state of the application
class App extends Component {
state = {
boardStatus: newBoardStatus(),
generation: 0,
isGameRunning: false,
speed: 500
};
// Other methods ...