Code Smell 155 - Multiple Promises

Code Smell 155 - Multiple Promises

You have promises. You need to wait. Wait for them all

TL;DR: Don't block yourself in a sorted way.

Problems

  • Indeterminism
  • Performance bottleneck

Solutions

  1. Wait for all promises at once.

Context

We heard about semaphores while studying Operating Systems.

We should wait until all conditions are met no matter the ordering.

Sample Code

Wrong

async fetchOne() { /* long task */ }
async fetchTwo() { /* another long task */ }

async fetchAll() {
  let res1 = await this.fetchOne(); 
  let res2 = await this.fetchTwo();
  // they can run in parallel !!  
}
        

Right

async fetchOne() { /* long task */ }
async fetchTwo() { /* another long task */ }

async fetchAll() {
  let [res3, res4] = await Promise.all([this.fetchOne(), this.fetchTwo()]);
  //We wait until ALL are done
}        

Detection

[X] Semi-Automatic

This is a semantic smell.

We can tell our linters to find some patterns related to promises waiting.

Tags

  • Performance

Conclusion

We need to be as close as possible to [real-world]((https://www.dhirubhai.net/pulse/what-wrong-software-maximiliano-contieri/) business rules.

If the rule states we need to wait for ALL operations, we should not force a particular order.

Credits

Thanks for the idea

Twitter

Photo by?Alvin Mahmudov?on?Unsplash

JavaScript is the only language that I'm aware of that people feel they don't need to learn before they start using it.

Douglas Crockford

Software Engineering Great Quotes

This article is part of the CodeSmell Series.

How to Find the Stinky parts of your Code

要查看或添加评论,请登录

Maximiliano Contieri的更多文章

  • Code Smell 295 - String Concatenation

    Code Smell 295 - String Concatenation

    Untangling the string mess in your code TL;DR: Avoid string concatenation for complex strings, use templates. Problems…

  • Code Smell 294 - Implicit Return

    Code Smell 294 - Implicit Return

    Your language adds clever features. Making YOU more obsolete TL;DR: Overusing implicit returns makes your code harder…

  • The Great Programmer Purge: How AI Is Taking Over the Tech Workforce

    The Great Programmer Purge: How AI Is Taking Over the Tech Workforce

    How AI is Redefining the Role of Programmers in the Tech Industry TL;DR: AI-generated code outperforms lazy…

    1 条评论
  • Refactoring 024 - Replace Global Variables with Dependency Injection

    Refactoring 024 - Replace Global Variables with Dependency Injection

    Break Hidden Dependencies for Cleaner Code TL;DR: Replace global variables with dependency injection to improve…

  • 10 More Simple Tips to Boost Your Productivity x2

    10 More Simple Tips to Boost Your Productivity x2

    The previous article on life hacks was a huge success. Let's start the year with more productivity tips! TL;DR: More…

  • Code Smell 293 - isTesting

    Code Smell 293 - isTesting

    Don’t let test code sneak into production TL;DR: Avoid adding isTesting or similar flags. Problems ?? Leaky abstraction…

  • Refactoring 001 - Remove Setters

    Refactoring 001 - Remove Setters

    Setters violate immutability and add accidental coupling TL;DR: Make your attributes private to favor mutability…

  • Code Smell 292 - Missing Return

    Code Smell 292 - Missing Return

    When your code loses its way TL;DR: Missing return statements cause unexpected behavior. Problems ?? Silent failures…

  • Code Smell 291 - Mutable Keys

    Code Smell 291 - Mutable Keys

    Changing Keys, Losing Values TL;DR: When you use mutable objects as keys in hashed collections, changing them breaks…

    2 条评论
  • Refactoring 023 - Replace Inheritance with Delegation

    Refactoring 023 - Replace Inheritance with Delegation

    Transform your rigid inheritance into flexible delegations TL;DR: Replace restrictive inheritance hierarchies with…

社区洞察

其他会员也浏览了