Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
How to solve ajax call delay?

I have an input field named location. When clicked on the search button, I want to take the input from the location field and using ajax to get the location details. The function getLocationData() returns the needed info, but the line

let locationData = getLocationsData();

in the click button function returns null. In the console, firstly, the console.log() from the click button function is called and then the console.log() from the getLocationData(). Can someone explain why it is called first? How can I solve this issue?

const baseUrl = 'https://booking-com.p.rapidapi.com/v1/hotels/';
var locale = "en-gb";

function getLocationData(){
    var destination = {};
    var location = $('#location').val();
    var route = "";
    if(location != null || location != ""){
        route = `locations?locale=${locale}&name=${location}`;
    }
    console.log(`${baseUrl}${route}`);
    $.ajax({
        method: "GET",
        url: `${baseUrl}${route}`,
        headers:{
            'x-rapidapi-host': 'booking-com.p.rapidapi.com',
            'x-rapidapi-key': 'api-key'
        }
    })
    .done(response => {
        if(response[0]["dest_type"] != "hotel"){
            destination = {
                "label":response[0]["label"],
                "dest_id": response[0]["dest_id"],
                "dest_type": response[0]["dest_type"]
            };
        }
        console.log("getLocationData()", destination);
        return destination;
    })
    .fail(response => {
        console.log("ajax failed",response);
    });
}


$('#search-hotels').click(function () {
    let dateRange = getDateRangeObject();
    let checkin = dateRange['checkin'];
    let checkout = dateRange['checkout'];
    let isCheckinInvalid = moment(checkin).isBefore(moment(new Date()).format('YYYY-MM-DD'));
    let isCheckoutInvalid = moment(checkout).isBefore(moment(new Date()).add(1, 'days').format('YYYY-MM-DD'));

    let adults = $('#adults').val();
    let rooms = $('#rooms').val();
    let isAdultsAndRoomValid = adults >= 1 && rooms >= 1;
    let locationData = getLocationsData();
    
    let destination = {...locationData};
    console.log("click button function",destination);

    if (location != "" && !isCheckinInvalid && !isCheckoutInvalid && isAdultsAndRoomValid) {
        window.location.href = `hotels.html?location=${location}&checkin=${checkin}&checkout=${checkout}&adults=${adults}&rooms=${rooms}`;
    }

    return false;
});
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!