Search code examples
bluebirdgm

Not able to bluebird.Promisifyall gm


The gm is giving the gm().write() expects a callback function error. The write function comes from fs so I also promisified it. Still it does not work.

var gm = bluebird.promisifyAll(require("gm"));
var fs = bluebird.promisifyAll(require("fs"));

gm(filePath).resize(null, 128).write(file)
    .then(function() {
        console.log("Done");
    })
    .catch(function(err) {
        console.log(err);
    });

How to use promise with gm?


Solution

  • Bluebird's normal scheme for promisifying with promisifyAll() creates .writeAsync() that returns a promise. It doesn't change .write() at all. This assumes that the object that gm() returns is something that Bluebird can get to via gm.prototype.

    So, you'd do this:

    const gm = require("gm");
    bluebird.promisifyAll(gm.prototype);
    
    gm(filePath).resize(null, 128).writeAsync(file).then(function() {
        console.log("Done");
    }).catch(function(err) {
        console.log(err);
    });
    

    Note: You don't have to promisify the fs module unless you're going to use the fs promisified methods directly yourself.