Reputation:
How to pass an additional parameter to handleFile
?
return fs.readFileAsync(path, 'utf8')
.then(handleFile)
.then(Process)
// ...
var handleFile = function (data) {
var keyVal = {};
data.split("\n").forEach(function (element) {
// ...
I handle vfile
get data from the readFileAsync
.
I need it to be something like (pseudo code):
return fs.readFileAsync(path, 'utf8')
.then(handleFile(newParam1,newParam2))
.then(Process)
// ...
var handleFile = function (data,newParam1,newParam2) {
Upvotes: 3
Views: 92
Reputation: 5296
return fs.readFileAsync(path, 'utf8')
.then(function(data) {
// get newParam1, newParam2 here or make sure they are available in closure
return handleFile(data, newParam1, newParam2);
})
.then(Process)
....
var handleFile = function (data,newParam1,newParam2) {
Upvotes: 0
Reputation: 1726
You can use .bind() for it. It is a way to do partial application and set the execution scope (which shouldn't be important in your case hence null
)
.then(handleFile.bind(null, newParam1,newParam2))
var handleFile = function (newParam1,newParam2, data,) { // ...
You can also implement yourself partial application without use of bind, then it will look like:
function handleFileWithParams(param1, param2) {
return function handleFile(data) {
// ... do stuff
}
}
.then(handleFileWithParams(param1, param2))
Lastly when you are already using library like lodash, you can use provided function _.partial which does exactly that.
Upvotes: 1