0

How do i call this response in another function? This is my code

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);      
      })
      .then(null, function(err) {
          // Log any errors.
          console.log(err);
      });
}

I want to pass the response of this function to another function.

For example:

$scope.sample = function(){
  //call the response of function queryCoreReportingApi
};
AyDee
  • 201
  • 1
  • 5
  • 13
  • is the "other function" calling this function, or is this function calling the "other function" - it's not clear from your question – Jaromanda X Oct 04 '16 at 02:55
  • `myFunction(response);` – Tushar Oct 04 '16 at 02:55
  • 2
    Possible duplicate of ["How do I return the response from an asynchronous call?"](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – nnnnnn Oct 04 '16 at 02:56
  • 1
    *I want to pass the response of this function* But that function has no response. You need to return its value. Then you don't "call a function response in another function"; you **access** that value, which is a promise, with `queryCoreReportingApi().then(response => $scope.sample = response)`. –  Oct 04 '16 at 03:22

1 Answers1

0

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.)

Alexander Nied
  • 12,804
  • 4
  • 25
  • 45
  • im sorry for my question not clear. for example i have this code. $scope.sample = function(){ //call the response of function queryCoreReportingApi }; – AyDee Oct 04 '16 at 03:12
  • myReportingService is not defined – AyDee Oct 04 '16 at 03:31
  • The chained `.then()` calls in the original code should be ok for ES6 promises (and other similar implementations). – nnnnnn Oct 04 '16 at 03:47