Friday, December 2, 2022

Promise with timeout

When you use await, it will wait until the Promise is either resolved or rejected. But sometimes API requests we make don’t get any response from the server.

You can use AbortController to abort one or more Web requests. The second way to solve this is to create a promise with a timeout.

Friday, November 25, 2022

Pipes and error handling

When using pipe(), the error events are not propagated automatically through the pipeline:

We will catch only the errors coming from stream2. 

If we want to catch any error generated from stream1 , we have to attach another error listener directly to it:

pipeline() helper function

You can use the pipeline() from the core stream package.

This pipes every stream passed in the arguments list to the next one. For each stream, it will also register a proper error and close listeners.

Friday, November 4, 2022

Vue3 - Attribute Inheritance

The child component by default inherit the declared attributes. Vue3 calls it the Fallthrough Attributes.

A "fallthrough attribute" is an attribute or v-on event listener that is passed to a component, but is not explicitly declared in the receiving component's props or emits. Common examples of this include class, style, and id attributes.

If you do not want a component to automatically inherit attributes, you can set inheritAttrs: false in the component's options. These fallthrough attributes can be accessed directly in template expressions as $attrs.


Wednesday, March 2, 2022

Optional object property with spread operator

I saw cool magic in my previous project. The optional object property.

Let see example:

Tuesday, February 22, 2022

Format Dates In JavaScript

Working with a date and time in JS was awful in the past. I dont know about you, but I used MomentJS or date-nfs library. 

Now it is possible to start using the Internationalization API for formating a date and time. Support is good in all browsers.

Let see an example:

If you want to use the browser’s locale, you must pass an empty Array in the constructor. The second argument is options. For more details see the documentation.

There is a Intl.RelativeTimeFormat() class for relative manipulation with a date and time. The Intl.RelativeTimeFormat is similar to the Intl.DateTimeFormat.

Let see an example:

Bonus :)

Friday, February 18, 2022

Streams - Transform

Transform streams are a special kind of Duplex stream. Transform streams apply some kind of transformation to each chunk of data that they receive from the Writable side, and then make the transformed data available on the Readable side.

For implementing a new Transform stream, we have to fill _transform() and optionally _flush() methods.

Let see example:

Thursday, February 17, 2022

Streams - Readable, Writable

Streams are one of the most important components of Node.js 

For example, the fs module has createReadStream() for reading from a file and createWriteStream() for writing to a file, the HTTP request and response objects are essentially streams, the zlib module allows compress and decompress data using a streaming interface and, crypto module exposes streaming primitives like createCipheriv and createDecipheriv.

Every stream in Node.js is an implementation of one of the four base abstract classes. Writable, Readable, Duplex, Transform. Each stream class is also an instance of EventEmitter.

Streams support two operating modes:

  • Binary mode - buffers or strings
  • Object mode: objects

Readable streams

A Readable stream represents a source of data. There are two approaches to receive the data from a Readable stream: non-flowing (or paused) and flowing. 

Paused mode

In paused mode, you need to call read() on the stream instance repeatedly until every chunk of data has been read.

The highWaterMark property, passed as an option to fs.createReadStream, determines how much data buffers inside the stream.

All Readable streams begin in paused mode but can be switched to flowing mode in one of the following ways: 

  • Adding a 'data' event handler.
  • Calling the stream.resume() method.
  • Calling the stream.pipe() method to send the data to a Writable.

Flowing mode

You can call readable.pause() and readable.resume()

Readable streams from iterables

You can create Readable stream instances from arrays or other iterable objects using the Readable.from()

Writable streams

A Writable stream represents a data destination. The example bellow shows an elegant way to create a gziped copy of a file.

In the next post, I am going to look at Transform streams.