Say you have some code like this:
functiongetData (cb) {
console.log("doing long async operation")
http.get('/user', cb)
}
getData(function(err, data) {
console.log("got 1")
})
getData(function(err, data) {
console.log("got 2")
})
doing long async operation
doing long async operation
got 1
got 2
getData
is a nice abstraction but every function that calls it will do an additional http request!
var getData = async.memoize(function(cb) {
console.log("doing long async operation")
http.get('/user', cb)
})
getData(function(err, data) {
console.log("got 1")
})
getData(function(err, data) {
console.log("got 2")
})
doing long async operation
got 1
got 2
async.memoize
saves the day!
What if getData takes an argument? Like getUserData(id, cb)?