Created
May 4, 2015 12:13
-
-
Save ekinertac/edd241e35fbdb076574b to your computer and use it in GitHub Desktop.
deferred angular request
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
params.callback = 'JSON_CALLBACK'; | |
// Create promise | |
var deffered = $q.defer(); | |
// Do request | |
$http({ | |
method: 'JSONP', | |
url: 'http://www.example.com/rest-api', | |
params: params, | |
cache: false | |
}).success(function(data) { | |
// Check results was found | |
if (data.status === 200 && data.results.length) { | |
// Resolve with success | |
deffered.resolve({ | |
status: 200, | |
results: data.results | |
}); | |
} else { | |
// Resolve with no content | |
deffered.resolve({ | |
status: 204 | |
}); | |
} | |
// If error then simply resolve with error | |
}).error(function() { | |
// Resolve with error | |
deffered.resolve({ | |
status: 500 | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment