var dataFetch= $q.defer();
function fetchData(){
return dataFetch.promise;
}
(function getData(){
setTimeOut(
function(){
myPromise.resolve("data");
}
,1000);
})();
fetchData().then(function(){alert("use old fetched data");});
The code above shows how "dataFetch" is defined outside the scope of the "getData" function, ensuring that a new promise is not created with each invocation of "getData".
"getData" will only be called once, and "dataFetch" will retain the data from the initial call without being updated.
Is this considered a promise anti-pattern? If so, what is the correct way to execute an asynchronous function just once?