afterfn
Execute a function after given function.
afterfn
Simply executes after the function is called.
afterfn.return
Allows manipulation of return value.
Examples
function doubleNumber(x) {
return x * 2
}
var doubleNumberAbs = after.return(doubleNumber, Math.abs)
var calledWith = []
var results = []
doubleNumberAbs = after(doubleNumberAbs, function fn() {
calledWith = calledWith.concat(fn.args)
results = results.concat(fn.value)
})
console.log(doubleNumberAbs(10))
console.log(doubleNumberAbs(-10))
console.log(doubleNumberAbs(-100))
console.log(calledWith)
console.log(results)
var myPackage = {
name: 'my-package',
version: 1,
build: function() {
},
bumpVersion: function() {
this.version++
}
}
myPackage.build = after(myPackage.build, myPackage.bumpVersion)
console.log('%s@v%s', myPackage.name, myPackage.version)
myPackage.build()
console.log('%s@v%s', myPackage.name, myPackage.version)
Facts
afterfn
returns a new Function.afterfn
ignores return value of after functionafterfn.return
allows manipulation of return value- Original return value will be passed as the first argument to the after function.
- Original arguments will be passed as the second argument to the after function.
- Original function will be passed as the third argument to the after function.
- Original function
this
context is maintained. - Properties and prototype are inherited though function arity will not be preserved.
See Also
License
MIT