Last active
January 21, 2017 11:01
-
-
Save felquis/f0dad3a5f98c6ccdc489c09569b337d8 to your computer and use it in GitHub Desktop.
Promise chain mutating Object with setTimeout, + fixing with R.merge
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
// Test 1, what will be displayed in the logs? | |
Promise.resolve() | |
.then(() => { | |
const car = { | |
color: 'Green' | |
} | |
setTimeout(() => { | |
car.color = 'Orange' | |
}, 0) | |
// Look at this | |
return car | |
}) | |
.then((car) => { | |
setTimeout(() => { | |
console.log(car) | |
}, 10) | |
console.log(car) | |
}) | |
// Test 2, with R.merge | |
Promise.resolve() | |
.then(() => { | |
const car = { | |
color: 'Green' | |
} | |
setTimeout(() => { | |
car.color = 'Orange' | |
}, 0) | |
// It will create a plain new object with no references to the old car object | |
return R.merge({}, car) | |
}) | |
.then((car) => { | |
setTimeout(() => { | |
console.log(car) | |
}, 10) | |
console.log(car) | |
}) | |
/* | |
Navigate to http://ramdajs.com/docs/#merge then open your DevTools | |
to run these blocks above. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment