When and how to apply timeout to JavaScript Promises

JavaScript promises do not have any time associated with them. We can use a .then() function and wait until the promise is resolved or rejected. We can even await it, and either of those works if the async task finishes in a reasonable amount of time. But in the case when the task is potentially taking a long time, we might want to let the user know. We want to apply timeout to JavaScript promises in such scenarios.

Fortunately, there is a JavaScript Promise combinator function that can help us with this:

Promise.race


Having consistency and enforcing coding standards becomes very important as an application scales. It becomes important to automate the process to ensure quality standards and make the application maintainable. ESLint and Prettier can be used to define these standards, but we need a tool to enforce them as well. Husky provides that functionality by providing pre-commit git hooks which can be configured as per our needs.

These standards can also be enforced by using gated checks on pull requests at the CI level, but Husky is an alternative to do it at the local machine level.

What is Husky?


If you have been using React for a while, you are familiar with how state update works. There are a lot of internal optimizations that React makes for faster rendering. And one of the implementation details of the React internals is that it does a check of whether the given state object has actually changed or not. But the behavior of assigning a new object/array trips up newcomers. Let us understand why does React need a new copy of an object/array when assigning the state.

Object.is() in JavaScript


TypeScript provides some built-in utility types that help facilitate transformations of types from one form to another.

These utilities are available globally. They can be quite handy in various situations.

TypeScript generics

type MyString = string;let helloWorldMessage: MyString = 'Hello Wisdom Geek';

Type generics are used to create reusable type aliases. Let us say we had an identity function which returns whatever value is passed back in:

Type generics are used to create reusable type…


Once we start exploring TypeScript, we start using interfaces and types without really understanding the differences between them. I am guilty of doing that as well. It is simple to get started with them and keep using them. But, at some point, it becomes important to know the difference between interface and type.

After all, we can use either of these:

interface Point {
x: number;
y: number;
}

or

type Point = {
x: number;
y: number;
}

The difference between interface and type is more than just the minor syntax declaration. Let us get deeper into it.

Type and type aliases


Writing conditional expressions is pretty easy to do. But there is room for improvement in the way we have been doing it. And with the flexibility that JavaScript provides, we can replace conditionals with clean code by using some good practices. And it can lead to more maintainable code. Let us take a look at writing better conditional expressions in this post.

1. Early returns


As you might know, I recently switched jobs. And in my long journey of finding a new one, there was a moment where I was offered way more money than I had anticipated for a role that I was not as passionate about. And I wish I could say that it was an easy no for me. But it was not. I wanted to say no. But instead, I kept asking myself, “Am I really going to do this?”. And I even talked to quite a few close friends about that decision. And everybody, including my parents, told me that…


Introduction of type hinting opened the gates for a lot of great new features in Python. And data validation and parsing became easier to do with the use of type hints. Pydantic is one such package that enforces type hints at runtime. It throws errors allowing developers to catch invalid data.

Pydantic not only does type checking and validation, it can be used to add constraints to properties and create custom validations for Python variables. It guarantees the types and constraints of the model have been applied and that the data is valid.

This is useful especially when we have…


Every now and then, you come across a JavaScript feature that vastly changes the way you write it. Destructuring, arrow functions, modules have been some of those features for me. Optional chaining is going to be the next one on that list for me.

Optional Chaining is in stage 4 of the ES2020 proposal, therefore should be added to the specification. It changes the way properties inside an object are accessed, especially the deeply nested ones. It is also available as a feature in TypeScript 3.7+.

The challenge


With the introduction of type hinting in Python 3.5, static typing and checking Python codes has started to gain popularity. While MyPy was the first tool to do static type checking of Python code, many other frameworks have been built after it. Pyright is one such tool built by Microsoft and is the one that we will be discussing in this post.

Type hints in Python

saransh kataria

For over and over, I have failed & in the process of losing it all, I realized that I might actually win.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store