In my honest opinion this question is not duplicated because I know how to fix it using a callback as explain in the linked question, this question is specific about a situation where I cannot modify the outfer function and this is not covered in the another question
I use a library that I cannot modify, the library do something like this:
var result = Foo()
Then it uses result
for the work. I can not modify how result
works, I can only modify Foo
because I pass it as parameter to the library.
If foo
has something like this
funcion Foo(){
return "Hello"
}
It works perfect. Result is Hello
and it works.
The issue is that my Foo
has an async call inside it. It calls a web server and then it should return the response of the server. So
funcion Foo(){
request.get('http://server.com', function(error, response, body){
var parsed = JSON.parse(body);
return parsed.Hello;
});
}
In this case when var result = Foo();
, result
does not contains the response of the server (because is async) and continue with an empty result.
So if I only can modify Foo
what should I do?
Update: If you dont find it possible I would love to hear alternatives like what should I ask in PR for the library, or a way to force the request to be syncronous