djake1680
5/8/2018 - 5:22 PM

nested_promise_example.js

getEmployeeSelectList(store, department_selected, position);

function getEmployeeSelectList(store, department_selected, position) {
    getEmployeeListData(store, department_selected, position).then(function (response) {
        console.log(response);
        getListOfEmployees(response[0], response[1]).then(function(response) {
        $('#employee_timeoff_list').html('');
                console.log(response);
                appendEmployeeList(response);
        })
    });
}

function getEmployeeListData(store, departmentSelected, position) {
    return new Promise(function(resolve, reject) {
        var storeArray = [];
        (store === '5' ? storeArray.push('5', '51') : storeArray.push(store));
        storeArray = JSON.stringify(storeArray);
        if((departmentSelected === 9010 || departmentSelected === 9939) && position === 'GENERAL MANAGER') {
            if(store !== '11') {
                resolve(['getGeneralManagerList', {'store_numbers': storeArray}]);
            }
            else {
                resolve(['getDivisionElevenList', {'department': departmentSelected, 'position': position}]);
            }
        }
        else if(store === '11' || store === '12') {
            if(departmentSelected === 8900 || departmentSelected === 8810) {
                var list = '';
                if(position === 'IT MANAGER') {
                    list = 'IT';
                }
                else {
                    list = "ALL";
                }
                resolve(['getCBOList', {'list': list}]);
            }
            else {
                resolve(['getDivisionElevenList', {'department': departmentSelected, 'position': position}]);
            }
        }
        else {
            resolve(['getEmployeesByEmployeeCodes', {'employee_code_array': departmentSelected, 'store_numbers': storeArray}]);
        }
    });
}
        
function getListOfEmployees(url, array) {
    return new Promise(function(resolve, reject) {
        console.log(array);
        $.post('employee_list/' + url, array, function (response) {
            try {
                response = JSON.parse(response);
                var employeeList = response.data;
                resolve(employeeList);
            }
            catch (error) {
                reject(error);
            }
        });
    });
}