11

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.

Damjan Pavlica
  • 31,277
  • 10
  • 71
  • 76
Dan Hlavenka
  • 3,697
  • 8
  • 42
  • 60

4 Answers4

14

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.

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' }
ThorSummoner
  • 16,657
  • 15
  • 135
  • 147
  • 1
    Thank you, I've been battling with this issue for hours and your explanation is so clear that I was able to finally make it work. – bitfidget Oct 19 '17 at 03:27
13

You can pass a different function for each iteration:

var getUpdatedFunction = function(folderName) {
    return function(event, filename) {
        log("CHANGED\t" + folderName + "/" + filename);
    };
};

for(i in channels) {
    foldername = 'share/channels/' + channels[i];
    fs.watch(foldername, {persistent: false}, getUpdatedFunction(foldername));
}
Scott Sauyet
  • 49,207
  • 4
  • 49
  • 103
7

You can use Function.bind:

function updated(extraInformation, event, filename) {
    log("CHANGED\t/share/channels/" + extraInformation + filename);
}

for(i in channels)
    fs.watch('share/channels/' + channels[i], {persistent: false},
              updated.bind(null, 'wherever/it/is/'));
Ry-
  • 218,210
  • 55
  • 464
  • 476
  • I don't follow your example at all. Did you even explicitly reference the extra parameter you added to the function? – ThorSummoner Jan 23 '15 at 23:11
  • @ThorSummoner: `updated.bind(null, 'wherever/it/is/')` returns a function that can take two arguments, prepend `'wherever/it/is/`, and then pass that to `updated` (with `null` being the unused value for `this`). – Ry- Jan 24 '15 at 00:33
  • This answer was heaven sent. – Proximo Dec 12 '17 at 09:26
0

You can pass additional callback in place

function updated(channel, filename) {
  log('CHANGED\t ' + channel + '/' + filename);
}

for(i in channels) {
  channel = '/share/channels/' + channels[i];
  fs.watch(channel, {persistent: false}, function (extra, event, filename) {
    updated(channel, filename);
  });
}
rusffer
  • 88
  • 8