I need to watch a small number of directories in a Node.JS application:
function updated(event, filename){
log("CHANGED\t/share/channels/" + filename);
}
for(i in channels)
fs.watch('share/channels/' + channels[i], {persistent: false}, updated);
The problem is that fs.watch only passes the filename to the callback function, without including the directory it's in. Is there a way I can somehow pass in an extra parameter to the updated() function so it knows where the file is?
I think I'm looking for something similar to Python's functools.partial
, if that helps any.
Doc: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind
Tip, the bound parameters occur before the call-time parameters.
my_hello = 'Hello!'
my_world = {
'antartica': 'cold',
}
anonymous_callback = function (injected1, injected2, param1, param2) {
param1 = param1 ? param1 : 'One';
param2 = param2 ? param2 : 'Two';
console.log('param1: (' + typeof(param1) + ') ' + param1)
console.log('param2: (' + typeof(param2) + ') ' + param2)
console.log('injected1: (' + typeof(injected1) + ') ' + injected1)
console.log('injected2: (' + typeof(injected2) + ') ' + injected2)
console.log(injected2)
}.bind(this, my_hello, my_world)
anonymous_callback('Param 1', 'Param 2')
Output:
param1: (string) Param 1
param2: (string) Param 2
injected1: (string) Hello!
injected2: (object) [object Object]
{ antartica: 'cold' }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With