I have created an application in angularjs promise in which i am calling a method within which a server call will be triggered, i have put a promise enclosing the server call, the server call execution will take some amount of time, after completing it will return the deferred object, everything is fine, but the promise is returning before the server call gets completed
I have mock up a similar scenario within a JSFiddle
function getServerCall() {
var deferred = $q.defer();
setInterval(function () {
$scope.$apply(function () {
deferred.resolve({
"success": true
});
});
}, 3000);
return deferred.promise;
}
console.log(JSON.stringify(getServerCall().success));
if (getServerCall().success)
{
console.log('Success');
}
else{
console.log('Failed');
}