This question already has an answer here:
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.
You can pass a different function for each iteration:
Example using JS Bind
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.
Output:
You can pass additional callback in place
You can use
Function.bind
: