Created
November 9, 2015 04:42
-
-
Save YashdalfTheGray/6d85e62af442f15cb2ab to your computer and use it in GitHub Desktop.
Creating a promise using $q
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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