Send a promise with two parameters

How do I pass an additional parameter handleFile

?

return fs.readFileAsync(path, 'utf8')
    .then(handleFile)
    .then(Process)
    // ...

var handleFile = function (data) {
    var keyVal = {};
    data.split("\n").forEach(function (element) {

// ...

      

I am processing vfile

receiving data from readFileAsync

.

I need it to be something like (pseudocode):

return fs.readFileAsync(path, 'utf8')
                .then(handleFile(newParam1,newParam2))
                .then(Process)

                // ...


var handleFile = function (data,newParam1,newParam2) {

      

+3


source to share


2 answers


You can use . bind () for it. This is a way to make a 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 a partial application without using a binding, then it will look like this:



function handleFileWithParams(param1, param2) {
    return function handleFile(data) {
        // ... do stuff
    } 
}

.then(handleFileWithParams(param1, param2))

      

Finally, when you are already using a library like lodash, you can use the provided _ function . partial , which does exactly that.

+1


source


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) {

      



0


source







All Articles