然后不是函数承诺错误

Jen*_*ton 2 javascript node.js promise bluebird

我是承诺的新手,我使用蓝鸟文档从异步代码中获取数据

我尝试的是以下内容:

错误是:

getToken.then 不是一个功能

我怎么能避免呢?

这个文件是connection.js

return connection.getToken.then(function(connToken){

   console.log(connToken);

}).catch({


})
Run Code Online (Sandbox Code Playgroud)

这是moduleB中的getToken代码

const request = require("request-promise");
const Promise = require("bluebird");
module.exports = {


    getToken: function () {


        return new Promise((resolve, reject) => {
            let options = {
                method: 'POST',
                url: 'https://authentication.arc.com/oauth/token',
                headers: {
                    grant_type: 'client_credentials',
                    authorization: 'Q0MDdmMCFiMTc0fGNvlQVRDWThDNDFsdkhibGNTbz0=',
                    accept: 'application/json;charset=utf-8',
                    'content-type': 'application/x-www-form-urlencoded'
                },
                form: {
                    grant_type: 'client_credentials',
                    token_format: 'opaque&response_type=token'
                }
            };


            request(options)
                .then(function (body) {

                    return body;
                })
                .catch(function (err) {
                    return err;          
                });
        })

    }
}
Run Code Online (Sandbox Code Playgroud)

Aro*_*ron 8

你需要实际调用该函数.

connection.js

return connection.getToken() // note the parentheses ()
  .then(function(connToken){
    console.log(connToken);
  })
  .catch(function(error){
    console.error(error);
  });
Run Code Online (Sandbox Code Playgroud)