This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Synchronous: 1,2,3 | |
alert(1); | |
alert(2); | |
alert(3); | |
// Asynchronous: 1,3,2 | |
alert(1); | |
setTimeout(() => alert(2), 0); | |
alert(3); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class App extends Component { | |
state = {...}; | |
runStopButton = () => {...} | |
handleClearBoard = () => {...} | |
handleNewBoard = () => {...} | |
handleToggleCellStatus = () => {...} | |
handleStep = () => {...} | |
handleSpeedChange = newSpeed => { | |
this.setState({ speed: newSpeed }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class App extends Component { | |
state = {...}; | |
runStopButton = () => {...} | |
handleClearBoard = () => {...} | |
handleNewBoard = () => {...} | |
handleToggleCellStatus = () => {...} | |
handleStep = () => { | |
const nextStep = prevState => { | |
const boardStatus = prevState.boardStatus; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class App extends Component { | |
state = {...}; | |
runStopButton = () => {...} | |
handleClearBoard = () => { | |
this.setState({ | |
boardStatus: newBoardStatus(() => false), | |
generation: 0 | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class App extends Component { | |
state = { | |
boardStatus: newBoardStatus(), | |
generation: 0, | |
isGameRunning: false, | |
speed: 500 | |
}; | |
// Other methods ... |
NewerOlder