This file contains 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
// Deep clone an object | |
const deepClone = JSON.parse(JSON.stringify(state)); | |
deepClone.tweets[3].tweet = 'Hijacking your tweets bruh!'; | |
// Origional tweet is unchanged | |
console.log(state.tweets[3].tweet); | |
// Cloned tweet is changed | |
console.log(deepClone.tweets[3].tweet); |
This file contains 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
// Change the tweet on our clone | |
shallowClone.tweets[3].tweet = 'Hijacking your tweets bruh!'; | |
// Changed :) | |
console.log(shallowClone.tweets[3]); | |
// Noooooo! Also change! | |
console.log(state.tweets[3]); | |
// Both properties reference the same object |
This file contains 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
// This is how you shallow clone an object | |
const shallowClone = Object.assign({}, state); | |
// Change the twitter handle | |
shallowClone.twitter = '@zuck'; | |
// Origional state isn't changed | |
console.log(state.twitter); // @JamesJefferyUK | |
// Changed |
This file contains 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
const state = { | |
twitter: '@JamesJefferyUK', | |
tweets: [ | |
{ | |
tweet: 'I <3 JavaScript', | |
read: false | |
}, | |
{ | |
tweet: 'React is so cooool', | |
read: false |
This file contains 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
[ | |
"nodejs ", | |
[ | |
[ | |
"\u003cb\u003enode js\u003c\/b\u003e \u003cb\u003etutorial\u003c\/b\u003e", | |
0, | |
[ | |
10 | |
], | |
{ |
This file contains 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
const util = require('util'); | |
// This function waits for an unknown amount of time | |
const wait = (delay, callback) => { | |
// setInterval returns an ID. We need this to stop the timer | |
const id = setInterval(() => { | |
// Generate a random number between 0 and 1 | |
const rand = Math.random(); | |
if (rand > 0.95) { |
This file contains 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
const wait = (delay, callback) => { /* … */ }; |
This file contains 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
const util = require('util'); | |
// Here we use util.promisify to convert the function to a promise | |
const waitAsync = util.promisify(wait); | |
//And here we use the promisified function. Cool huh? | |
waitAsync(1000) | |
.then(data => console.log(data)) | |
.catch(err => console.error(`[Error]: ${err}`)); |
This file contains 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
node promisify.js | |
Waiting ... | |
Waiting ... | |
Waiting ... | |
Waiting ... | |
Waiting ... | |
Waiting ... | |
Waiting ... | |
Waiting ... |
This file contains 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
node promisify.js | |
Waiting ... | |
Waiting ... | |
Waiting ... | |
Waiting ... | |
Waiting ... | |
Congratulations, you have finished waiting. |