0

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?

1 Answers1

1

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.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • I tried your solution but it is giving `gm(...).resize(...).writeAsync is not a function` error. –  Aug 31 '16 at 08:23
  • @SharadCodes - Then, maybe `gm()` is not directly compatible with Bluebird's `.promisifyAll()`. I'm not familiar with that library specifically to know what it returns after `gm().resize()`. Apparently whatever that object is is not getting promisified. – jfriend00 Aug 31 '16 at 08:44
  • I tried `var gm = require("gm"); Promise.promisifyAll(gm.prototype); ` then it worked. Why it worked is completely over me. –  Aug 31 '16 at 08:55
  • @SharadCodes - What version of Bluebird are you running. The [Bluebird doc I read here](http://bluebirdjs.com/docs/api/promise.promisifyall.html), says that `.promisfyAll()` already promisifies the prototype for you. – jfriend00 Aug 31 '16 at 09:14
  • I am using version 3.4.3. –  Aug 31 '16 at 09:20