When you perform an HTTP request, this request is not completed instantly, but rather it's completed asynchronously. So what happens is that when the request is made you get a sort of token (a Promise) that you can keep track of while the request is 'in the air'.
This promise is the object you log when you type:
var u = $http.get("http://localhost:8580/jettyjspconfiguration-example/sql?loc=locc");
console.log(u);
To 'keep track' of this promise you can supply it with functions, somewhat similar to event handlers by using the then
, error
, success
, and finally
functions.
So here's what happens:
// Start the request and get a promise that an answer will eventually come.
var u = $http.get("http://localhost:8580/jettyjspconfiguration-example/sql?loc=locc");
// The request is handled asynchronously, so all we have now is the promise that
// at some time there will be a result.
console.log(u);
// Assign 'event handlers' to the promise
u.then(function(result) {
// This 'event handler' function is called when the async process completes
// successfully. You can now use the data as you please
doFancyStuffWithResultData(result.data);
}, function(error) {
// This 'event handler' function is called when the async process has an error
console.error('Ohnoz, things went wrong', error);
});
Note that I put 'event handler' in quotes because it helps to think of the functions as like-'event handlers', but there are some differences. Have a look at the documentation for the $q service for more information on what Promises are and how they work.