The context is not completely clear from your question, but I'm going to take a crack at it anyway.
Firstly, before answering your question, I have concerns about the way your code is formed above-- in particular, the two .then()
blocks you have chained. While I don't know the specific API you are using, generally promise-type APIs allow .then()
methods to be passed two arguments-- the first a function to be called if the promise succeeds, and the second a function to be called in the case of an error. So I am suspecting that perhaps what you meant to type was:
function queryCoreReportingApi(profileId) {
// Query the Core Reporting API for the number sessions for
// the past seven days.
gapi.client.analytics.data.ga.get({
'ids': 'ga:' + profileId,
'start-date': '7daysAgo',
'end-date': 'yesterday',
'metrics': 'ga:pageviews',
'dimensions' : 'ga:date'
})
.then(function(response) {
console.log(response);
},
function(err) {
// Log any errors.
console.log(err);
});
}
This is just a hunch, but I think you should check that.
Now in regards to the question you asked-- unless I am missing some important information, you should be able to access any method available in the same scope (or higher) as the queryCoreReportingApi
method and pass it the response from the success method of your .then
. For instance:
function myCustomFunction(data) {
//do some stuff with the data
}
function queryCoreReportingApi(profileId) {
// Query the Core Reporting API for the number sessions for
// the past seven days.
gapi.client.analytics.data.ga.get({
'ids': 'ga:' + profileId,
'start-date': '7daysAgo',
'end-date': 'yesterday',
'metrics': 'ga:pageviews',
'dimensions' : 'ga:date'
})
.then(function(response) {
console.log(response);
myCustomFunction(response);
},
function(err) {
// Log any errors.
console.log(err);
});
}
Does this answer your question? If not, please provide more details as to the specifics of your inquiry.
EDIT: I see from your edit above that it looks like you are using Angular. In that case, I am also going to surmise that the pasted method you have provided is inside of a service that is being called from the controller. If that is the case, you can expand your queryCoreReportingApi
method to accept a callback method as the second argument, to be called in the event of a success, like so:
// in your controller
$scope.sample = function (data) {
// manipulate the returned data
}
// call the service
myReportingService.queryCoreReportingApi(profileId, $scope.sample);
And then later, in your service:
//in your service
function queryCoreReportingApi(profileId, callback) {
// Query the Core Reporting API for the number sessions for
// the past seven days.
gapi.client.analytics.data.ga.get({
'ids': 'ga:' + profileId,
'start-date': '7daysAgo',
'end-date': 'yesterday',
'metrics': 'ga:pageviews',
'dimensions' : 'ga:date'
})
.then(function(response) {
console.log(response);
callback(response);
},
function(err) {
// Log any errors.
console.log(err);
});
}
(I also do see that you are referring to "calling" the response of the success. That sort of suggests that the response is itself a method you want to call. I think that is just a mistype on your part, but if that is the case, it is as simple as just calling it when you receive it, like so:
.then(function(response) {
response();
});
But I doubt this is what you meant.)