How to Use Promises in JavaScript

Promises are a powerful tool for dealing with asynchronous code in JavaScript, allowing you to avoid the callback hell. In this blog post, we will explore how to use promises, including creating promises, consuming promises, chaining promises, handling errors, and orchestrating promises. Introduction to Promises A promise is a proxy for a value that will eventually become available. It provides a way to work with asynchronous code without writing too many callbacks....

How to Use Python reduce() to Calculate Values from a Sequence

When working with collections in Python, there are three global functions that can be quite useful: map(), filter(), and reduce(). Tip: In some cases, using list comprehensions may be more intuitive and is generally considered more “pythonic”. In this blog post, we will focus on the reduce() function, which is used to calculate a single value from a sequence, such as a list. Let’s consider a scenario where you have a list of expenses stored as tuples, and you want to calculate the sum of a specific property in each tuple (in this case, the cost of each expense):...

How to Use Python's filter() Function

In Python, there are three global functions that can be quite useful when working with collections: map(), filter(), and reduce(). In this blog post, we will focus on how to use the filter() function. Note: In some cases, using list comprehensions can be a more intuitive and Pythonic approach. The filter() function takes an iterable as input and returns a filter object, which is another iterable that contains only the items that pass a given filtering condition....

How to Use Python's map() Function

In Python, there are three useful global functions - map(), filter(), and reduce() - that we can use to work with collections. map() is specifically used to apply a function to each item in an iterable, such as a list, and create a new list with the same number of items, but with modified values. Let’s take a look at an example that demonstrates the use of map(). Suppose we have a list of numbers [1, 2, 3], and we want to double each number in the list....

How to Use Redis Hashes for Storing Object-like Items

In previous articles, we learned about using Lists and Sets to associate a key with a single value or a group of values. However, there are cases where we need to associate multiple values with a single key, such as when storing object-like items. This is where Redis Hashes come in handy. Let’s take an example of a person who has a name and an age. We can create a Redis Hash called person:1 and associate the name and age with it using the HMSET command:...

How to Use Redis Sorted Lists

Redis Sorted Lists are a powerful data structure that associates a rank with each item in a set. While they are similar to regular sets, they use different commands and provide additional functionality. To start using Redis Sorted Lists, you’ll need to use the ZADD command instead of SADD. When using ZADD, you provide a score along with the value: ZADD names 1 "Flavio" ZADD names 2 "Syd" ZADD names 2 "Roger" As shown in the example, the values in the list must still be unique, but they are now associated with a score....

How to Use Redis with Node.js

One of the most popular libraries for working with Redis server in a Node.js application is node-redis, which can be found at https://github.com/NodeRedis/node-redis. To install the library in your project, run the following command: npm install redis Tip: If your project is brand new and does not have a package.json file, make sure to run npm init -y first. Connecting to the Redis Instance Once the library is installed, require it in your project using:...

How to Use Sequelize to Interact with PostgreSQL: A Comprehensive Guide

When it comes to working with databases, you have the option to directly use the primitives provided by the database or leverage a library that abstracts away the intricate details for you. Sequelize is a popular Node.js library that serves as a wrapper for PostgreSQL, MySQL, and various other databases. In this blog post, we will explore how to use Sequelize specifically with a PostgreSQL database. Install and Configure Sequelize Under the hood, Sequelize utilizes the pg library to establish a connection with PostgreSQL....

How to Use Supabase as Your PostgreSQL Hosting

In this blog post, we will explore how you can utilize Supabase as your PostgreSQL hosting solution. Supabase is not only a database hosting service but also a comprehensive app development platform built on top of PostgreSQL. With Supabase, you can take advantage of features like free connection pooling to prevent exhausting the database connection limit with tools like Prisma. Plus, Supabase offers a free account that supports up to two projects, making it an excellent choice for trying it out or working on multiple projects simultaneously....

How to Use SWR: A Short Tutorial

In a Next.js app, one of the most effective ways to perform a GET request is by using SWR. To get started, you can install SWR by running the following command: npm install swr After installing SWR, you need to define a fetcher function. I usually create a lib/fetcher.js file and include the following code: const fetcher = (...args) => fetch(...args).then((res) => res.json()); export default fetcher; Make sure to import the fetcher function at the top of your component’s file:...