I am confused about the example of context parameter from documents

23 views
Skip to first unread message

log系統帳號 b

unread,
Sep 20, 2016, 11:42:27 AM9/20/16
to Google API JavaScript Client
I am confused about the example from here: https://developers.google.com/api-client-library/javascript/features/promises#context-parameter,

It is about context parameter.

I found I never have a chance to know when the results are ready.

Take the following example,

I add console.log(personFetcher.results); to the end of script, it always return empty, because it is asynchronous.

So, my problem is how can I access the results, when it's ready?


var personFetcher = {
  results
: [],

  fetch
: function(name) {
    gapi
.client.request({path: '/plus/v1/people', params:{query: name}}).then(function(response) {
     
this.results.push(response.result);
   
}, function(reason) {
      console
.error(name, 'was not fetched:', reason.result.error.message);
   
}, this);
 
}
};
personFetcher
.fetch('John');

log系統帳號 b

unread,
Sep 20, 2016, 8:21:28 PM9/20/16
to Google API JavaScript Client
Forgot about it, I do it through the normal promise way.
Reply all
Reply to author
Forward
0 new messages