0

I wanna make a GET request from Bluemix API with request-promise and the promise doesn't do anything. I'm able to get data, but It's asynchronous. It's possible that I'm just misunderstanding some things. Does anyone know how to make it wait few seconds?

var request = require('request');
var rp = require('request-promise');

var username = "username";
var password = "password";
var location = 'location';
var language = 'cs-CZ';
var units = 'm';
var locationURL = 'https://twcservice.mybluemix.net/api/weather/v3/location/search'
console.log('url:', locationURL);

var qs = {
  language: language,
  query: location
};
var options = {
  uri: locationURL,
  qs: qs,
  auth: {
    username: username,
    password: password
  },
  headers: {
    'User-Agent': 'Request-Promise'
  },
  json: true // Automatically parses the JSON string in the response 
};
var jsonPosition = {
  location: []
};

rp(options)
.then(function (repos) {
    var jsonPosition = repos;
    console.log(jsonPosition); //I can receive the data, but can't work with them
return repos;
})
.catch(function (err) {
    // API call failed... 
});

console.log('options:', options);
//console.log(jsonPosition);

I can get repos (jsonPosition), which is this code below, but It pop up one second after "//console.log(jsonPosition);" so I can't use it in code. I need to find way how to slow it down.

{ location: 
   { address: 
      [ 'Praha, Praha, Česko',
        'Praha, Česko',
        'Praha, Banskobystrický kraj, Slovensko' ],
     adminDistrict: [ 'Praha', 'Praha', 'Banskobystrický kraj' ],
     adminDistrictCode: [ null, null, null ],
     city: [ 'Praha', null, 'Praha' ],
     country: [ 'Česko', 'Česko', 'Slovensko' ],
     countryCode: [ 'CZ', 'CZ', 'SK' ],
     displayName: [ 'Praha', 'Praha', 'Praha' ],
     ianaTimeZone: [ 'Europe/Prague', 'Europe/Prague', 'Europe/Bratislava' ],
     latitude: [ 50.089, 50.06, 48.381 ],
     locale: [ [Object], [Object], [Object] ],
     longitude: [ 14.421, 14.446, 19.527 ],
     neighborhood: [ null, null, null ],
     placeId: 
      [ '81cbe8a06fd80171651aef7a414bce1e867b5282a9600401442fc37ce54868ce',
        '2496f2ca6bdc13f9c1b4e1d83e78a40602f387ae3244fba51f059be17d224eb1',
        '9294b774d158cdfad77c4a20c39559523ce0628e89025917922169775db07ef3' ],
     postalCode: [ null, null, null ],
     postalKey: [ null, null, null ] } }
Blank
  • 145
  • 3
  • 18
  • Where do you call `main(options, repos)`? – CodingNagger Aug 18 '17 at 14:05
  • @IamNguele Sorry, I posted wrong version of code. It should be okay now... – Blank Aug 18 '17 at 14:11
  • No worries, what is `rp`? – CodingNagger Aug 18 '17 at 14:13
  • @IamNguele That's imported module ( var rp = require('request-promise'); ) – Blank Aug 18 '17 at 14:18
  • I tried to run that code and got a `StatusCodeError: 401 - "Unauthorized."` error message so it might just be the username or the password are invalid or not passed properly. – CodingNagger Aug 18 '17 at 14:24
  • @IamNguele well they won't be 'username' and 'password' like in the code, will they? – schrej Aug 18 '17 at 14:47
  • @Lupp What does `console.log(jsonPosition);` output? The code looks correct in my opinion. Also, add `console.error(err)` in the catch part of the promise as there might be errors during the request. – schrej Aug 18 '17 at 14:51
  • @IamNguele I didn't wanted to provide credentials to my account. I posted what i get from URL. – Blank Aug 18 '17 at 14:51
  • instead of `return repos;` why not just call a function and pass in `repos` data and handle it there? – Andrew Lohr Aug 18 '17 at 14:57
  • Why do you want to make it wait a few seconds? And why can't you use it in code? – baao Aug 18 '17 at 14:58
  • @schrej console.log(jsonPosition); outputs posted code, but commented (//console.log(jsonPosition);) will give me { location: [] }´and outputs before ( this one console.log(jsonPosition); ) So I guess that is still asynchronous. – Blank Aug 18 '17 at 15:01
  • Well with a promise it is going to stay asynchronous. Why don't you just handle the data in the then callback? – schrej Aug 18 '17 at 15:04
  • @AndrewLohr I have much longer code and I need to get 2 values from this URL to make it work property. – Blank Aug 18 '17 at 15:07
  • seems like you are misunderstanding the scoping of the variable `jsonPosition` and how asynchronous code works. Maybe look up some resources on those. your comment `//console.log(jsonPosition);` is printing out `jsonPosition` before the request even comes back (hence why it is the same as you initialized it before the request) – Andrew Lohr Aug 18 '17 at 15:14
  • @AndrewLohr So, how do I synchronize it? I thought that I can do it with .then() – Blank Aug 18 '17 at 15:16
  • right .then does that which is why `console.log(jsonPosition); //I can receive the data, but can't work with them` actually works and prints out your data. You need to continue using the variable in that .then function scope, either by calling another function and passing the variable or chaining another .then https://javascript.info/promise-chaining – Andrew Lohr Aug 18 '17 at 15:18
  • @AndrewLohr and there is no way how to get it from .then? I have a problem with it, because I need to use promise two times... and It's possible that I will recive data from user, so I won't need to use this function at all... so it's problem for me to keep it in .then – Blank Aug 18 '17 at 15:23
  • Simply chain the other promise in another then, or use Promise.all. Have a look at the duplicate question, it'll show you the basics. For all the rest, there's plenty of documentation, e.g. on bluebirds page – baao Aug 18 '17 at 15:43
  • @baao But I need to somehow resolve it. I tried to use it and I can't return value back to the application from .then – Blank Aug 18 '17 at 16:07

0 Answers0