So after reading up I see that I need a closure or to use bind but I am not fully clear on the proper syntax and which I should use.
I am using the imagemagick module from https://github.com/rsms/node-imagemagick and need to pass the filename to the resize callback.
Syntax is:
var filename = 'myfile.jpeg';
im.resize(options, callback(err, stdout, stderr))
Now I want my callback to accept extra parameters such as the filename.
I would like to be able to call another function and have the filename along with the 3 callback parameters (also note that this is in a loop so the filename is from an array - not sure if that matters)
So I have a callback function:
var callback = function(err, stdout, stderr) {
//how do I access filename?
}
Can I get an explanation of which I should use (closure or bind) and pros/cons and, if a closure, how would I actually write the callback function - I am unclear on how I access err, stdout and stderr in that way.
You can do it with an extra anonymous function if that's what you mean:
im.resize(options, function(err, stdout, stderr) {
callback(err, stdout, stderr, filename) // do something with filename
});
I'd say you could use closure, in your callback you could do something like this:
var filename = 'myfile.jpg';
im.resize(options, function(err,stdout,stderr) {
console.log('filename',filename,'err',err);
});