question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Web code style: Use async/await?

See original GitHub issue

Would you like to try changing the coding style from using Promise.then into async/await? Thanks

One example of change (on src/lib/api.ts):

private getRandomSentences(count: number): Promise<string[]> {
  return this.getSentences().then(sentences => {
    let randoms = [];
    for (var i = 0; i < count; i++) {
      let distribution = Random.integer(0, sentences.length - 1);
      let randomIndex = distribution(this.randomEngine);
      randoms.push(sentences[randomIndex]);
    }
    return randoms;
  });
}

become:

private async getRandomSentences(count: number): Promise<string[]> {
  const sentences = await this.getSentences();
  let randoms = [];
  for (var i = 0; i < count; i++) {
    let distribution = Random.integer(0, sentences.length - 1);
    let randomIndex = distribution(this.randomEngine);
    randoms.push(sentences[randomIndex]);
  }
  return randoms;
}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
mikehenrtycommented, Oct 23, 2017

We are now using async/await in multiple places, and will continue to use it going forward. Let’s incrementally switch to this new API when we touch parts of the code rather than doing large refactorings.

With that in mind, I’m closing this bug as we won’t have separated work for this.

2reactions
mikehenrtycommented, Oct 2, 2017

Is there any particular reason for (keeping on) using bluebird? Or could we switch the server code to plain ES6 Promises as well?

I’m fine with removing bluebird now that we are targeting es6.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Updating callback-style code to use async/await
This code uses a callback function to perform an asynchronous task. The problem with writing asynchronous code like this is maintainability.
Read more >
Understanding Async Await - CSS-Tricks
One thing (a)waits for another​​ One common use of Async/Await is to use it to chain multiple asynchronous calls. Here, we'll fetch some...
Read more >
Async/Await vs Promise.then Style - Jesse Warden
I wanted to cover what both style offers, why you'd use one or the ... the internet, reading files, they block, or pause...
Read more >
Async/await - The Modern JavaScript Tutorial
asynchronous code returns a value, but it wraps the value in a Promise, so that the caller can use it to specify the...
Read more >
How to use promises - Learn web development | MDN
Note: In this article, we will explore promises by copying code samples from the page into your browser's JavaScript console.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found