Skip to content
This repository was archived by the owner on Nov 20, 2025. It is now read-only.
This repository was archived by the owner on Nov 20, 2025. It is now read-only.

Refactoring: Quit mixing Promises and async/await #1434

@shogo-nakano-desu

Description

@shogo-nakano-desu

Problem

Suggestion

  • It would be better to use async/await only and reduce the length of code. It will make this repo easier to see through.
  • I am going to create PR when this change is accepted but beforehand, I put my plan below. Please use these code for your decision.

Before

// TODO: refactor the below code so that it doesn't mix and match
      // promises and async/await.
      this._getDefaultProjectIdPromise = new Promise(
        // eslint-disable-next-line no-async-promise-executor
        async (resolve, reject) => {
          try {
            const projectId =
              this.getProductionProjectId() ||
              (await this.getFileProjectId()) ||
              (await this.getDefaultServiceProjectId()) ||
              (await this.getGCEProjectId()) ||
              (await this.getExternalAccountClientProjectId());
            this._cachedProjectId = projectId;
            if (!projectId) {
              throw new Error(
                'Unable to detect a Project Id in the current environment. \n' +
                  'To learn more about authentication and Google APIs, visit: \n' +
                  'https://cloud.google.com/docs/authentication/getting-started'
              );
            }
            resolve(projectId);
          } catch (e) {
            reject(e);
          }
        }
      );
    }

After

this._getDefaultProjectIdPromise = (async () => {
        const projectId =
          this.getProductionProjectId() ||
          (await this.getFileProjectId()) ||
          (await this.getDefaultServiceProjectId()) ||
          (await this.getGCEProjectId()) ||
          (await this.getExternalAccountClientProjectId());
        this._cachedProjectId = projectId;
        if (!projectId) {
          throw new Error(
            'Unable to detect a Project Id in the current environment. \n' +
              'To learn more about authentication and Google APIs, visit: \n' +
              'https://cloud.google.com/docs/authentication/getting-started'
          );
        }
        return projectId;
      })();
    }

Metadata

Metadata

Labels

priority: p3Desirable enhancement or fix. May not be included in next release.type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions