Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ajhekman/8341211 to your computer and use it in GitHub Desktop.
Save ajhekman/8341211 to your computer and use it in GitHub Desktop.
var yourAsyncTask;
yourAsyncTask = function() {
var deffered, failure, successful;
deffered = $q.defer();
successful = 'what ever your success condition is';
failure = 'what ever your failure condition is';
/*here we're using setTimeout to make it asynchronous*/
setTimeout(function() {
var result;
/* Asume longRunningTask is defined elsewhere*/
result = longRunningTask();
if (result === successful) {
deffered.resolve(result);
}
if (result === failure) {
return deffered.reject(result);
}
});
/*deffered.promise returns immediately*/
return deffered.promise;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment