Web code style: Use async/await?
See original GitHub issueWould 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:
- Created 6 years ago
- Comments:10 (10 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
I’m fine with removing
bluebird
now that we are targeting es6.