I am writing an asynchronous javascript function that will be called by consumers to get certain data. Below is a simple implementation that I wrote at the beginning (removing errors and removing other things for clarity).
function getData(callback){ if (data is available as a JavaScript object){ callback(data); }else{ getAsyncData(function(data){
It is important to note that getData can quickly return data if the data is already available as a JavaScript object.
I want to replace this implementation with one that returns a promise object to the caller. This script shows an example implementation - http://fiddle.jshell.net/ZjUg3/44/
Question. Since getData can return quickly, can it be possible when getData resolves the promise before the caller has established a chain of handlers using the then method? Just to simulate this, in the fiddle, if I call and then the method inside the setTimeout function (with zero delay), the callback will not be called. If I call the then method outside the setTimeout function, callback is called. I am not sure if this is even a valid concern or a valid usecase. I am completely new to angularjs development and will be grateful for your views :)
source share