Skip to content
This repository was archived by the owner on Mar 21, 2021. It is now read-only.
This repository was archived by the owner on Mar 21, 2021. It is now read-only.

Use async functions instead of promises #447

@mrts

Description

@mrts
Overview of the feature request

Use async/await instead of promises for more succinct code.

For example, instead of the following JHipster-generated code

    this.rideService()
      .retrieve(paginationQuery)
      .then(
        res => {
          this.rides = res.data;
          this.totalItems = Number(res.headers['x-total-count']);
          this.queryCount = this.totalItems;
          this.isFetching = false;
        },
        err => {
          this.isFetching = false;
        }
      );

do this:

  try {
    const res = await this.rideService().retrieve(paginationQuery);
    this.rides = res.data;
    this.totalItems = Number(res.headers['x-total-count']);
    this.queryCount = this.totalItems;
  } finally {
    this.isFetching = false;
  }

Note that I've left out catch for purely aesthetic reasons - it is still actually required.

Motivation for or Use Case

Quoting Google Developers async functions primer:

Async functions are [...] quite frankly marvelous. They allow you to write promise-based code as if it were synchronous, but without blocking the main thread. They make your asynchronous code less "clever" and more readable.


  • Checking this box is mandatory (this is just to show you read everything)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions