4

I'm new to promises and I use the bluebird docs to get data from async code

what I tried is the following:

the error is:

getToken.then is not a function

How can I avoid it?

This file connection.js

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

   console.log(connToken);

}).catch({


})

This the code of getToken in moduleB

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;          
                });
        })

    }
}
Soviut
  • 88,194
  • 49
  • 192
  • 260
Jenny Hilton
  • 1,297
  • 7
  • 21
  • 40
  • 1
    What's your question? – Aron Jul 05 '17 at 14:45
  • @Aron - sorry update the question with the error... – Jenny Hilton Jul 05 '17 at 14:46
  • 2
    Apart from the obvious typo (missing method call), avoid the [`Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it) and [Drop the pointless `.then(body => body)`](https://stackoverflow.com/q/41089122/1048572)! – Bergi Jul 05 '17 at 14:49

1 Answers1

12

You need to actually call the function.

connection.js

return connection.getToken() // note the parentheses ()
  .then(function(connToken){
    console.log(connToken);
  })
  .catch(function(error){
    console.error(error);
  });
Aron
  • 8,696
  • 6
  • 33
  • 59
  • Still Im not getting the data .... – Jenny Hilton Jul 05 '17 at 14:51
  • are you still getting the same error? – Aron Jul 05 '17 at 14:52
  • No but the console log doesnt print the data ... so I guess I someing wrong in the promise in the getToken function, can you take a look please? – Jenny Hilton Jul 05 '17 at 14:53
  • put a `console.log` or `console.error` in the `catch` handler too so you can see what's going on. I've updated my answer. – Aron Jul 05 '17 at 14:55
  • I dont get any erorr do you the cosole log are called before the I got the data from the promise, any idea why ? maybe the getToken was not using well promises ? – Jenny Hilton Jul 05 '17 at 14:57
  • @JennyHilton You're not `resolve`ing your promise at any point. And neither should you, because you don't need a wrapping promise, since `request` already produces a promise. – deceze Jul 05 '17 at 14:57
  • @deceze - so can you please show me what I should fix in the gettoken function? – Jenny Hilton Jul 05 '17 at 14:58
  • 1
    @Jenny `function getToken() { return request(...); }` - That is all you need. – deceze Jul 05 '17 at 15:03