Skip to content

Instantly share code, notes, and snippets.

@felquis
Last active January 21, 2017 11:01
Show Gist options
  • Save felquis/f0dad3a5f98c6ccdc489c09569b337d8 to your computer and use it in GitHub Desktop.
Save felquis/f0dad3a5f98c6ccdc489c09569b337d8 to your computer and use it in GitHub Desktop.
Promise chain mutating Object with setTimeout, + fixing with R.merge
// 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