Currently, I am in the process of creating an asynchronous JavaScript function that will be utilized by users to retrieve specific data. Here is a basic outline of the implementation I have come up with initially:
function getData(callback){
if (data is available as a JavaScript object){
callback(data);
}else{
getAsyncData(function(data){
//some transformations on data
callback(data);
});
}
}
An important point to consider is that the getData function has the ability to provide data swiftly if it is already accessible as a JavaScript object.
My goal is to upgrade this implementation to one that returns a promise object to the caller. A demo of this new approach can be seen in this fiddle - http://fiddle.jshell.net/ZjUg3/44/
The question I have is - Given that getData is capable of fast response times, is there a scenario where getData resolves the promise even before the caller establishes a handler chain using the then method? To simulate this, in the fiddle, if I invoke the then method within a setTimeout function (with zero delay), the callback does not trigger. However, when I call the then method outside of the setTimeout function, the callback does get triggered. I am uncertain if this concern is valid or applicable. As someone who is relatively new to AngularJS development, I would greatly appreciate your insights :)