Monday, January 31, 2022

Promises chaining with reduce

Hi devs,

I am going to made a promises chain with reduce function in this post. I think it is an elegant solution to the sequential execution of async tasks.

"Each .then() returns a newly generated promise object, which can optionally be used for chaining."
MDN Web Docs

Let's see example:

Now let's make chaining with reduce:

Friday, January 28, 2022

Eager & lazy Promise

Promises are always eager to the execution process. It means that when a Promise is created, the callback is immediately executed. That is ok, but sometimes you need call Promise in specific time, for example after setting some resources. 

The sollution is to wrap a Promise in a function.

Lets look an example:

Happy codding.

Wednesday, January 26, 2022

Conccurency Task queue

Spawning parallel tasks without control can often lead to excessive load. For instance sending bulk emails or downloads a lot of files slightly exceeds server resource limit.

In this post I am going to make a parallel tasks queue with concurrency limit control

This queue extends EventEmitter. It is possible to receive errors and get a message after finish.

Let see a queue:

Let see a examle:

Happy coding.


Tuesday, January 25, 2022

Try/ Catch Wrapper

Hey Guys,

do you also have a lots of Try/ Catch in the code? Of course yes. In my previous project I saw interesting wrapper for that.

Let see it:

Happy codding!

Thursday, January 13, 2022

Arguments in arrow function in JavaScript

Yes, that is true. You canot use the arguments object in arrow function. But if you want to access  the arguments object in an arrow function, you can use the rest parameters feature.

See the example bellow:

Happy coding.

Friday, January 7, 2022

Conditional async import in JavaScript

In this post I am going to talk about conditional import module in JavaScript ( ECMAScript modules ).

The import statemen in JavaScript is static and therefore has two limitation:

  1. a module identifier cannot be constructed at runtime
  2. have to be declared at the top level of file and therefore cannot be nested within control flow statement like if statement.

In case you need conditional import you can use async dynamic import. Async import can be performed at runtime using special import() function.

The module identifier can be any module identifier supported by static import.

Let see example:

This form also supports the await keyword: