1

Essentially when i call my function getToken() it should return the bearer + token from the api.

The problem I have is that due to the asynchronous process that happens, the data is not returned instantly; so in reading the following resource:

My understanding is that I need to return my response in the form of a promise, and set a timeout to ensure that the return accounts for the time it takes for the server to send back my request in the form of a response.

var request = require('request-promise');
var time = require('timers');
class Auth {
 getToken() {
   let options = {
     method: 'POST',
     uri: 'https://example.com/service/ep',
     body: {
       username: 'someUser',
       password: 'somePass'
     },
     json: true
   }
  request(options)
    .then(function (body) {
      // console.log(body)
      return new Promise((resolve) => {
        time.setTimeout(() => {
          resolve(body)
        },3000)
    });
  })
  .catch(function (err) {
    return err
  });
 }
}
module.exports = new Auth

Unfortunately when i run my program in the node repel, it returns nothing and it does not appear to wait; of course when i log my response 'console.log(body)', it appears meaning there must be something wrong with how i'm returning my promise; i'm quite new to the likes of promises.

Could use with a second pair of eyes.

  • How are you calling `getToken()`? Are you using `.then()` when you call it properly? And forget timeouts like that, they're not what you need and would be very fragile. – Matti Virkkunen May 01 '18 at 09:31
  • The aim is to use getToken() to return the bearer token, so that i can make other requests to test the API. I figure the timeouts are probably not the best approach. – Owen Carter May 01 '18 at 09:33
  • You definitely shouldn't be relying on a setTimeout, the promise is supposed to resolve (and your then called) when the response is received. I'm assuming this is for node otherwise I would question why you're not using whatwg-fetch? – Dominic May 01 '18 at 09:34
  • Because in theory your code seems like it should work (albeit with a 3 second delay), could you post the part where you try to call that function? – Matti Virkkunen May 01 '18 at 09:34
  • At the moment i just call the function in the node repel (command line): node like so: var data = require('file'); data.getToken(); Do I need to do something with the call? – Owen Carter May 01 '18 at 09:37
  • How do you know it doesn't wait? You don't do anything with the result after the timeout has resolved the promise. – Lennholm May 01 '18 at 09:42
  • 1
    You forgot to `return` the promise, i.e. `return request(options)` – Lennholm May 01 '18 at 09:44

1 Answers1

3

My understanding is that I need to return my response in the form of a promise, and set a timeout to ensure that the return accounts for the time it takes for the server to send back my request in the form of a response.

No. You need to return a promise (request already gives you one) and then the code you return the promise to needs to expect a promise (and call then() on it to get the data).

You don't need any time delays.


var request = require('request-promise');
var time = require('timers');
class Auth {
 getToken() {
   let options = {
     method: 'POST',
     uri: 'https://example.com/service/ep',
     body: {
       username: 'someUser',
       password: 'somePass'
     },
     json: true
   }
  return request(options);
 }
}
module.exports = new Auth

const auth = require("Auth");
auth.getToken().then(function (data) {
    console.log(data);
});
Matti Virkkunen
  • 63,558
  • 9
  • 127
  • 159
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • I misunderstood how to use promises, at least I thought I was handling it in the first function. since then i've written another function to try and handle the response. `makeAnotherRequest() { let token; this.getToken().then(function (data) { token = data; console.log('response from the server: ', data); }).catch(function (error) { console.log('error ', error) }); request.get('https://example.com/service/ep/207', { 'auth': { 'bearer': `${token}` } }); }` currently working on handling error case. – Owen Carter May 01 '18 at 09:47