jQuery Deferred

by Craig Boland

HTML

<select id='select1'>
    <option>Opt 1</option>
    <option>Opt 2</option>
</select>

JavaScript

function getEPList() {
    console.log('Retrieving EP list from server...');
    
    // AJAX call to server with filter options
    var epList = [201508, 201507, 201506];
    
    var $select = $('#select1');
    
    // Clear select list
    $select.find('option').remove();
    
    $.each(epList, function(idx, ep) {
        $select.append(new Option(ep, ep, false, idx === 0));
    });
    
    // Create a promise for the returned data
    var dfd = new $.Deferred();
    
    console.log('Retrieving EP list complete.');
    
    // Return the promise
    return dfd;
}

function refreshGrid() {
    console.log('Refreshing grid data from server.');
    
    console.log('Refreshing grid data complete.');
}

$.when(
    getEPList()
)
.then(refreshGrid())
.then(function() {
    console.log('Done!');
});