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

Use async functions instead of promises #447

Open
1 task done
mrts opened this issue Aug 29, 2019 · 9 comments
Open
1 task done

Use async functions instead of promises #447

mrts opened this issue Aug 29, 2019 · 9 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@mrts
Copy link

mrts commented Aug 29, 2019

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)
@atomfrede
Copy link
Member

Seems like a good idea. Would you mind doing a PR for that? @jhipster/vuejs-developers wdyt?

@atomfrede atomfrede added enhancement New feature or request good first issue Good for newcomers labels Sep 2, 2019
@mrts
Copy link
Author

mrts commented Sep 2, 2019

Possibly - let's see how far I get

@antonioortizpola
Copy link
Contributor

@mrts have you found time to work on this?

If not, I could take it to work with in the next one or two weeks.

@pascalgrimaud
Copy link
Member

I don't know Vue.js enough to tell if it's the right way or not.
Cc @sahbi-ktifa @hdurix @nonomoho : I need your opinion here
Maybe @Gnuk you can help too ? What do you think

@nonomoho
Copy link
Member

I discussed it with @Gnuk:
Both solutions are good but we don't like try/catch so much ...

@mrts
Copy link
Author

mrts commented Sep 17, 2019

@antonioortizpola nope, sorry, I don't currently have time. Please go ahead - and good luck :)! Note that you have to use try/catch, try/finally is incomplete.

@mrts
Copy link
Author

mrts commented Sep 17, 2019

@nonomoho glad you like this, but can you elaborate why you don't like try/catch?

@nonomoho
Copy link
Member

It's juste personal, I think then syntax is more understandable!
But it's just my opinion 😉

@antonioortizpola
Copy link
Contributor

Coming from C# await seems way more natural to me, what is the "standard" or the more used way in JS and Vue?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

5 participants