Skip to content

Instantly share code, notes, and snippets.

@codejockie
Last active January 24, 2020 07:49
Show Gist options
  • Select an option

  • Save codejockie/6aaafb701e8bbe492041d2aada133385 to your computer and use it in GitHub Desktop.

Select an option

Save codejockie/6aaafb701e8bbe492041d2aada133385 to your computer and use it in GitHub Desktop.
const memoize = require('memoize')
const date = memoize(function(seed, cb) {
setTimeout(function() {
cb(null, Date.now(), seed)
}, 100)
})
date({ name: "John", username: "codejockie" }, function(err, d1, seed1) { // given a set of arguments
console.log(d1, seed1) // 1304606051552
date({ name: "John", username: "codejocki" }, function(err, d2, seed2) { // same arguments
console.log(d2, seed2) // 1304606051552 cached! same results
date({ name: "Kennedy", sex: "Male" }, function(err, d3, seed3) { // we changed the arguments
console.log(d3, seed3) // 1304606051652 so it's different
})
})
})
function fakeApi(param, cb) {
setTimeout(function() {
cb(null, param)
}, 100)
}
const memoized = memoize(function(config, cb) {
fakeApi(config, (err, res) => {
cb(err, res)
})
})
memoized({ name: "John", username: "codejockie" }, function(err, d1) {
console.log(d1)
date({ name: "John", username: "codejocki" }, function(err, d2) {
console.log(d2)
date({ name: "Kennedy", sex: "Male" }, function(err, d3) {
console.log(d3)
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment