My js code needs to wait until ajax is finished to load the data.
if (district_search_value[0] == true) {
        var zone_search_value = checkZone(district_search_value[1], datas[i]['recipient_zone']);
        console.log(zone_search_value);
    }
The checkZone function :
   function checkZone(district_id, zone_name) {
    var value = [];
    value[0] = false;
    value[1] = 0;
    find_zones_from_district_id().then(function(data) {
        // Run this when your request was successful
        console.log(data);
        var zones = JSON.parse(data);
            //console.log(zones);
            for (zones_row = 0; zones_row < zones.length; zones_row++) {
                if (zone_name == zones[zones_row]['zone_name']) {
                    value[0] = true;
                    value[1] = zones[zones_row]['id'];
                }
            }
        return value;
    }).catch(function(err) {
        // Run this when promise was rejected via reject()
        console.log(err);
    })
}
The main promise function :
function find_zones_from_district_id(district_id) {
    return new Promise(function (resolve, reject) {
        $.ajax({
            type: 'POST',
            url: 'ajaxData_for_user.php',
            data: {import_excel_find_zones_from_district_id: 'district_id'},
            success: function (data) {
                resolve(data) // Resolve promise and go to then()
            },
            error: function (err) {
                reject(err) // Reject the promise and go to catch()
            }
        });
    });
}
What is the reason behind javascript not waiting for promise to resolve ? I've also tried the $.when in jquery but this also does not wait for ajax to load data.
 
    