Latests posts in Learning

Simple Redux

This is a post that tries to explain the the basics of Redux. We’ll build a minimal working example with Redux. If you’re looking for proper Redux documentation then check official docs.

Read more

Mastering JS Patterns

This is the page about Mastering JavaScript Design Patterns: A Practical Introduction to Building Better Applications.

Read more

JavaScript Chain of Responsibility pattern with Promises

Chain of Responsibility is a handy pattern for having multiple entities that could resolve a certain task. These entities get a chance to resolve the task one by one until one of them does that (or all fail).

Definition

Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it. There is a potentially variable number of "handler" objects and a stream of requests that must be handled. Need to efficiently process the requests without hard-wiring handler relationships and precedence, or request-to-handler mappings.

Let’s go with an example. Let’s say that your application displays youtube videos. Initially you have only the video ID. Your Chain of Responsibility is composed from:

  • Application cache (a plain JS Object)
  • Browser cache
  • Your server cache (you want to save on bills)
  • YouTube API (last resort)

One important thing to take into account is that some requests may be asynchronous (the last 2). So we have to use callbacks or Promises. Actually using Promises we’ll be able to handle rejections and errors.

Read more