Widget's client side function is not waiting for server response and returning undefined

Nikhil65
Mega Guru

When user clicks on a button, I have invoked a client side function which is making a server function call using c.server.get, but its not waiting for the response from server side.

I checked using alert and can confirm that the server function is returning true/false but the client side function test() is returning undefined since its  not waiting for server function to return response.

 

if(test()){
        logic 1
} else {
        logic 2
}

//Client side function
function test(){
c.server.get({
       action: "checkIssue"
        }).then(function(response) {
		return response.data.checkIssueVar;
        });
}

//Server side function
if (input.action == 'checkIssue') {
	//some logic 
	return data.checkIssueVar;
}

 

 

1 ACCEPTED SOLUTION

Please check article: https://stackoverflow.com/questions/34094806/return-from-a-promise-then, you will need to understand how does promise work then you will know how to rectify your code

View solution in original post

3 REPLIES 3

DYCM
Mega Sage

 

 

function test() {
	c.server.get({
			action: "checkIssue"
		})
		.then(function (response) {
			if(response.data.checkIssueVar){
                logic 1
            }
            else{
                logic 2
            }
		});
}

test();
​

 

Basically the issue you are facing is a typical javascript promise issue, please refer to this article to understand how does it work: https://stackoverflow.com/questions/34094806/return-from-a-promise-then

Though this could have been a possible solution but unfortunately this wont do, since the logic that I have to run is also dependent on other functions, PSB:

if(test() && test2() && test3()){
    logic1;
}else{
    logic2;

}

For ease of understanding I had shown 1 function call in the example I had shared earlier:
Now in this case since my other function always returns undefined at first, hence the else part runs always

Please check article: https://stackoverflow.com/questions/34094806/return-from-a-promise-then, you will need to understand how does promise work then you will know how to rectify your code