Search code examples
javascriptangular-promisegoogle-closure-compiler

Error - inconsistent return type (with closure compiler Promise)


I'm trying to write a function that makes an API call and returns a Promise. Here's my function definition:

  /**
   * Gets the IAM policy for a service account. Wraps getIamPolicy endpoint:
   * https://cloud.google.com/iam/reference/rest/v1/projects.serviceAccounts/getIamPolicy
   * @param {!Project} project
   * @param {string} email - Unique email for a service account.
   * @return {!angular.$q.Promise<!Object<!string, !Policy>>}
   */
  getIamPolicy(project, email) {
    const path = constructPath_(project, email) + ':getIamPolicy';
    return this.apiClient_.request({method: 'POST', path}, this.config_)
        .then(response => { debugger; });
  }

I'm using the closure-compiler, and that throws a compile error:

service-account-service.js:124: ERROR - inconsistent return type
found   : angular.$q.Promise<undefined>
required: angular.$q.Promise<Object<string,Policy>>
    return this.apiClient_.request({method: 'POST', path}, this.config_)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

What am I doing wrong? How should I be returning the promise?

The previous functions I've written using my apiClient_.request helper function work fine. I should be returning the same value from this.apiClient_.request.

Policy and Project are defined in an externs file (I believe correctly). I'm also using Angular 1.4


Solution

  • The Promise<undefined> that the error says you're returning is correct. While the this.apiClient_.request helper function is returning a Promise, the return type of the Promise (the <undefined> bit) is being overridden by .then(response => { debugger; }) code. ie, that code doesn't have a return statement, so it's returning undefined!

    So it worked when I changed my code to:

    return this.apiClient_.request({method: 'POST', path}, this.config_)
        .then(response => {
          debugger;
          return response;
         });