I have a function that queries the DOM and returns an array of data attributes.
var getUIDs = function () {
    $list = $('.foo');
    if ( $list.length ) {
        // loop through all the .foos and build an array
        return fooArray;
    } else {
        setTimeout(function(){
            getUIDs()
        }, 500);
    }
}
This function can sometimes be called prior to .foo being present in the DOM. That's why if I check every half second or so, within a few seconds the array will exist and I could send it back.
My question is, is there an established pattern I should follow that allows a function to be called, but will not receive a return value until there is one?
 
     
     
     
    