This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 9 months ago.
I tried to do this, but it doesn't work. Any help is appreciated thanks!
export class OuterClass {
let isEffectiveUrl = (url:string) => {
let tmpRes:boolean
this.http.get(url).subscribe((result) => {
tmpRes = Object.keys(result).length > 0;
});
return tmpRes;
}
}
You're setting isEffectiveUrl to be a function, not the resulting evaluation. One way to do this would create a local variable and then execute your http.get within a function or one of your lifecycle hooks.
e.g.
export class OuterClass {
isEffectiveUrl: boolean = false;
testUrl(url: string): void {
this.http.get(url).subscribe((result) => {
this.isEffectiveUrl = Object.keys(result).length > 0;
});
}
}
Just remember, this is a reactive model. So what's being run in the subscribe block is asynchronous.
Related
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 2 years ago.
So I have this javascript code. There are 2 nested functions. How can I return a value to funcA() inside funcB() ?
function funcA() {
funcB(() => {
//return funcA inside here
})
}
Is this even possible without doing it like the following?
function funcA() {
let returnValueA;
funcB(() => {
//change returnValueA inside here
})
return returnValueA;
}
In short: No.
A return statement only defines what is returned from the function it is part of.
This question already has answers here:
What do multiple arrow functions mean in JavaScript?
(7 answers)
Closed 3 years ago.
I am studying about the React using textbook. There are some codes I cant understand in the book.
const loggerMiddleware = store => next => action => {
}
I know the anonymous function in the javascript.
(a, b, c) => {
}
But, what is this?
store => next => action => {
}
please help me.
It's a higher-order function, i.e. a function that returns a function. It means the same as
store => next => {
return action => { }
}
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
Closed 3 years ago.
I have to create a number of RabbitMQ channels in succession, so I am trying to parameterize their creation in a function whose return value would be the channel. The function is declared in the callback-of-a-callback to the connection function.
var otaChannel = null;
amqp.connect(amqp_url, (err0, conn) => {
if(err0) {
throw(err0);
}
function getConsumerChannel(channelname) {
conn.createChannel((err1, ch) => {
if(err1) {
throw(err1);
}
ch.assertQueue(channelname, '', (err, q) => {
console.log(' [*] Created ' + q.queue + '.');
return ch;
});
});
}
otaChannel = getConsumerChannel(otaQName);
});
If I do "console.log(ch)" in the assertQueue function, I see the details of the channel. However, otaChannel in the final line of the code is always null. I've tried a bunch of variations of the code I posted, but cannot get the channel assigned to otaChannel. Where am I going wrong?
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 4 years ago.
I am using the fetch library and I am trying this:
fetchtest() {
fetch('someurl...').then((data) => {
if (data.status === 200) {
return true;
}
});
}
then to call it:
myMethod() {
if (this.fetchtest()) {
console.info('Would return true');
}
}
It's not returning anything.
What I'm I doing wrong?
This is happening because this.fetchtest() is returning a Promise which is 'truthy' in nature.
It returns a promise so it may be not present at the time it is needed to be used.
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 6 years ago.
How I can return a string from a javascript callback
I have two functions, main function is working on loaded.
and another function is used to calling web service.
I would like to know how can JS return the string value to main function.
thanks
function thisCallJSON(webServiceURL) {
var params = {};
params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.JSON;
params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.GET;
gadgets.io.makeRequest(webServiceURL, function(response)
{
if(response.data && response.text)
{
var jsondata = response.data;
for (var key in jsondata)
{
var value = jsondata[key];
//alert("Member Name : "+value["memNm"]);
}
}
else
{
//alert("Member Name : Not Found !");
}
}, params);
}; function main(){
var txt_string = "";
txt_string = thisCallJSON("http://192.100.1.59");
}
You can assign the value to the variable in the scope of the main function, but it won't happen before the main function is finished executing because of the event loop. Instead, you should put your code inside the callback, or better yet, look at how you would use javascript promises to accomplish this.