Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error myFunction(...).then is not a function

I have the following module that basically performs a GET request to Google:

// my-module.js
var request = require('request');
var BPromise = require('bluebird');

module.exports = get;

function get() {
    return BPromise.promisify(doRequest);
}

function doRequest(callback) {
    request.get({
        uri: "http://google.com",
    }, function (err, res, body) {
        if (!err && res.statusCode == 200) {
            callback(null, body);
        }
        else {
            callback(err, null);
        }
    });
}

And I want to use this module like so:

//use-module.js
var myModule = require('./my-module');

myModule().then(function (body) {
     console.log(body);
});

The error I'm facing is the following:

myModule(...).then is not a function.

What am I doing wrong?

like image 972
curpickled Avatar asked Jun 20 '26 15:06

curpickled


1 Answers

BPromise.promisify(doRequest) does not call doRequest, but returns a "promisified" version of that function. You should do that once, not at each call. This should work:

module.exports = BPromise.promisify(doRequest);
like image 152
Sergey Salnikov Avatar answered Jun 22 '26 03:06

Sergey Salnikov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!