kentor
kentor

Reputation: 18544

PromisifyAll - Callback is not a function

Either I missunderstood how BlueBird and it's promisify stuff works or I am doing something wrong here. I've got a "upload-handler" which exports one function. This function has a callback

The upload-handler looks like this (simplified):

function processSourceStrings(fileInformation, callback) {
    var filePath = fileInformation.path
    var fileExtension = path.extname(filePath)
    switch(fileExtension) {
        case '.json':
            processFile(filePath, function(err, result) {
                if(err)
                    callback(err)

                callback(null, result)
            })

        case '.pot':
        case '.po':
            processFile(err, result) {
                if(err)
                    callback(err)

                callback(null, result)
            })
    }
}

module.exports = {
    processSourceStrings: processSourceStrings
}

In my router I promisify the handler like this:

const uploadHandler = Promise.promisifyAll(require('./process-uploads/upload-handler'))

When the function is called at runtime (when it processes a file) it throws an exception on the callback(err) line, which says:

TypeError: callback is not a function

Here I am calling the function from my router.js:

    for(var i=0; i<req.files["sourceStrings"].length; i++) {
        var fileInformation = req.files["sourceStrings"][i]
        var filePath = fileInformation.path
        var targetFilePath = path.join(targetPath, req.files["sourceStrings"][i].filename)
        fileInformation.path = targetFilePath


        mv(filePath, targetFilePath, {mkdirp: true}).then(uploadHandler.processSourceStrings(fileInformation))
            .then(function(result) {
                console.log(result)
            })
            .catch(function(err) {
                next(err)
            })
    }

What am I doing wrong?

Upvotes: 1

Views: 1383

Answers (1)

t.niese
t.niese

Reputation: 40872

uploadHandler.processSourceStrings(fileInformation) is a call to the regular callback based function and this expects a callback as second parameter.

Promise.promisifyAll:

Promisifies the entire object by going through the object's properties and creating an async equivalent of each function on the object and its prototype chain. The promisified method name will be the original method name suffixed with suffix (default is "Async").

So you would call the promisified version this way:

 uploadHandler.processSourceStringsAsync(fileInformation)

Upvotes: 2

Related Questions