简体   繁体   English

错误myFunction(...)。然后它不是一个函数

[英]Error myFunction(…).then is not a function

I have the following module that basically performs a GET request to Google: 我有以下模块基本上向Google执行GET请求:

// 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? 我究竟做错了什么?

BPromise.promisify(doRequest) does not call doRequest , but returns a "promisified" version of that function. BPromise.promisify(doRequest)不会调用doRequest ,而是返回该函数的“promisified”版本。 You should do that once, not at each call. 你应该这样做一次,而不是每次通话。 This should work: 这应该工作:

module.exports = BPromise.promisify(doRequest);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM