Skip to content

Instantly share code, notes, and snippets.

@YashdalfTheGray
Created November 9, 2015 04:42
Show Gist options
  • Save YashdalfTheGray/6d85e62af442f15cb2ab to your computer and use it in GitHub Desktop.
Save YashdalfTheGray/6d85e62af442f15cb2ab to your computer and use it in GitHub Desktop.
Creating a promise using $q
function getFile(url) {
// create a deferred object.
var def = $q.defer();
// NOTE
// the $http methods success() and error()
// have been deprecated in favor of using then()
// from the standard Promises API. These are
// used here to show how to create a promise.
$http.get(url)
.success(function(data, status) {
// resolve the promise with good data.
def.resolve({ data: data, status: status });
}).error(function(data, status) {
// reject the promise if something went wrong.
def.reject({ data: data, status: status });
});
// return the promise object.
return def.promise;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment