Can't then a promise return by a function - jimp
<p>I am using the <a href="https://github.com/oliver-moran/jimp" rel="nofollow">Jimp</a> library to do some image manipulation. Stepping through the <code>read</code> method I see the last line hit is the one returning a promise. As soon as it returns, the <code>.then</code> throws an error as though the promise was never returned from <code>read</code>.</p>

<p>The error: <strong>Cannot read property 'then' of undefined</strong></p>

<p>Source code from jimp (clearly returning a promise):</p>

<pre><code>Jimp.read = function(src, cb) {
    var promise = new Promise(
        function(resolve, reject) {
            cb = cb || function(err, image) {
                if (err) reject(err);
                else resolve(image);
            }
            if ("string" != typeof src &amp;&amp; ("object" != typeof src || !Buffer.isBuffer(src)))
                return throwError.call(this, "src must be a string or a Buffer", cb);
            var img = new Jimp(src, cb);
        }
    );
    return promise;
}
</code></pre>

<p>My code: </p>

<pre><code>var jimp = require('jimp');
var nano = require('nano');
var _ = require('underscore');
var Common = require('../models/Common');
var util = require('../utils/helper');
var fs = require('fs');

module.exports.changeLogo = function changeLogo(user, file) {
    var theFile = file.destination + file.filename; //file.path?

    jimp.read(theFile).then(function(logo) {
        fs.unlink(theFile);
        logo.resize(jimp.AUTO, 150)

        logo.getBuffer(file.mimetype, function(err, buffer) {

            return Common.addAttachment(user, 'logo' + util.getFileExtension(file.filename), buffer, file.mimetype);
        })
    })
    .catch(function(err) {
        console.log(err);
    })

};
</code></pre>

<p>I break before running jimp.read(theFile), and run this in console:</p>

<pre><code>var test = jimp.read(theFile);
test;
</code></pre>

<p>This gives me <code>#&lt;Promise&gt;</code></p>

<p>typeof test here is an object.</p>