Click here to Skip to main content
16,019,983 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I am using this function to acquire some data from a DB and store the result in a class stripLayout. However the problem is that during the first run the class remains empty, so I introduced the async: false and solved the problem.

But I was reading that this is not the right way to do it, and 'promise' can be used instead. I could not find a way that makes this code work when I tried to do such a change. Can anyone help me out please?

function getStripLayoutsDetails(){
	$.ajax( 'admnStripLayouts_getStripLayoutDetails.php',
	{
		async: false,
		type: 'POST',
		data: {					
			LayoutName: $('#aSL_stripLayoutsList').val()					
		},
		success: function (result) {

		stripLayout = {
			layoutName:result.stripLayoutDetails[0].LayoutName,
			version:result.stripLayoutDetails[0].Version
		};				
							
		if(result.status == "Error"){
			displayError(result.detail, 'Error Retrieving Strip Layout Details', 'critical');
			return;
			}					
		},							
		error: function (resp, status, xhr) {
			displayError('Valid Value lookup request to server failed', 'Lookup Error', 'critical');
		}
	}); 
}


What I have tried:

Tried this change, however this is behaving similar to when not having async: false

function getStripLayoutsDetails(){
    $.ajax(
        { url: "/admnStripLayouts_getStripLayoutDetails.php" ,
            type: 'POST',
            data: {
                LayoutName: $('#aSL_stripLayoutsList').val()
            }
    })
    .done(function(getStripLayoutsDetails) {
        stripLayout = {
            layoutName:getStripLayoutsDetails.stripLayoutDetails[0].LayoutName,
            version:getStripLayoutsDetails.stripLayoutDetails[0].Version
    };
}).fail(function(e) {
    displayError('Valid Value lookup request to server failed', 'Lookup Error', 'critical');

    }).always(function() {} );
}
Posted
Updated 28-Sep-17 5:21am
v2
Comments
Karthik_Mahalingam 28-Sep-17 2:13am    
you are not returning any data so why do you need to use async: false,
datt265 28-Sep-17 2:28am    
Yes I am getting the result as object and store in a class
Karthik_Mahalingam 28-Sep-17 3:20am    
use  Reply  button, to post Comments/query to the user, so that the user gets notified and responds to your text.
Karthik_Mahalingam 28-Sep-17 3:21am    
what is the exact issue?
is there an issue with async or some other?
datt265 28-Sep-17 3:38am    
I have a list box, then when I click an item I retrieve the information by calling this function. At startup I am initializing a public class, when I click an item in the list I call this function that will fill the class. The problem is that the first item I click the class is still blank, then I click on another item an the class will contain the information related to the previously clicked item.

1 solution

The problem is not with the getStripLayoutsDetails function. The problem is in the calling function.

You've already modified the getStripLayoutsDetails function to use a promise. Now you need to return a promise from that function, and rewrite the calling function in a similar fashion.

Instead of:
JavaScript
getStripLayoutDetails();
// Use stripLayout here...

You would have:
JavaScript
function getStripLayoutsDetails(){
    return $.ajax(...).done(function(getStripLayoutsDetails) { ... }).fail(function(e) { ... });
}

getStripLayoutsDetails().done(function(){
    // Use the stripLayout here...
});

Promises[^]
Promises/A+[^]
JavaScript Promises ... In Wicked Detail - Matt Greer[^]
Deferred Object | jQuery API Documentation[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900