0

JS newbie here so perhaps I don't even realize how to word this question properly, my apologies.

A 3rd party function by default uses callback to make an HTTP request and return a value. Below is simplified sample from the doc:

someObj.someFunc(params, function(err, data) {
    console.log(data);           // successful response 
});

The problem: async approach doesn't work here because I need data before I can continue. I try to convert in to non-async version (the library allows it):

GetData = someObj.someFunc(params);
ret = GetData.send()

console.debug(ret);

It is ok so far: I'm getting the response: enter image description here

data is null here but from this answer I know it is ok, and if I expand the Object (without reloading page or anything like that) I see the necessary value:

enter image description here

Now the problem is: I cannot get data value!

console.debug(ret.data);  // returns null

I tried to debug with JSON.stringify() but ret is cyclic object. I tried this solution to dump the cyclic object but the answer shows no data.

How should it be done? How can I get the data.ShardIterator value?

Putnik
  • 5,925
  • 7
  • 38
  • 58
  • It would help to know exactly what API you're using, but it's generally true that you **cannot** make an asynchronous API into a synchronous API. Via the relatively new `async` and `await` facilities you can write code that looks superficially like synchronous code, but it's still actually asynchronous. – Pointy Jan 01 '19 at 16:57
  • This is a common predicament of race conditions with async requests. Look into async/await for how to handle. The rest of the program continues while your request has not yet responded, so it's undefined, which means you need to execute your code in the handler block, or by using *await* (async request handler) – vol7ron Jan 01 '19 at 16:57
  • "_async approach doesn't work_" It will work, you just have to design your code to work in the asynchronous enviroment. – Teemu Jan 01 '19 at 16:57
  • @Pointy: it does work, I see http status 200 from subsequent request. – Putnik Jan 01 '19 at 16:59
  • @Teemu: could you link to an example, please? – Putnik Jan 01 '19 at 16:59
  • You need to wait for the request to complete before accessing `ret.data` – Paul Jan 01 '19 at 17:01
  • As you said "I need data before I can continue", so you need to find a way to wait for the data. Not sure about your API, but you can try `GetData.send().then(res => { console.log(res.data); });` – Ray Chan Jan 01 '19 at 17:04
  • @RayChan `TypeError: GetData.send(...).then is not a function`. What am I doing wrong? – Putnik Jan 01 '19 at 17:12
  • @Putnik yes the asynchronous operation will work, but the notion of returning the result as if the API were synchronous will *not* work. – Pointy Jan 01 '19 at 17:14
  • @Putnik That means GetData.send() does not returns a promise. Maybe you can just put your logic that depends on the response data inside the callback function for `someObj.someFunc`. And the [post about handling async call above](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) should be a good read to solve your issue. – Ray Chan Jan 01 '19 at 17:42

0 Answers0