teleren/devto huggingface dataset:
My Understanding of Event Loop in NodeJS
Tags: adc
Published: 2020-01-23 21:46:49

# My Understanding of Event Loop in NodeJS

By definition, the event loop in Node JS is what makes it possible for a Node JS to handle multiple requests at the same time without waiting/blocking other requests.

Almost all operating systems nowadays are multithreaded, which means they can handle a number of different tasks at the same time. For instance, a Linux system like fedora or ubuntu can perform an I/O operation such as writing to disk and at the same time fetching data from a remote server or streaming a video online, this ability of the operating system to handle multiple tasks at the same is what NodeJS event loop uses on to delegate tasks to the host OS.

So, how does NodeJS do it exactly
## How Node JS Event loop makes use of the base operating system to serve multiple requests
NodeJS Event loop offloads tasks to the host operating system and in turn, the operating system provides a callback as an observer for when it has finished a certain task. This happens so fast that NodeJS thread is always available for new requests.

Sometimes, there are tasks that require intensive calculations and the base operating system can’t handle them or it takes longer to complete, in this case, it is when NodeJS’s thread pool comes into play. By default when you create a thread pool, it’ll have four threads assigned and you can use these to handle those tasks the OS can’t handle.

What is Node Js thread pool
## Understanding the event loop(main loop) and thread pool (worker pool) in Node JS
In Node Js, there are two types of threads, the main thread also known as event thread and thread pool in which Node JS uses to handle CPU intensive tasks.

As mentioned earlier in this article there comes a time when the base OS can’t handle a specific task, and this is when the thread pool is used to handle those tasks.

After reading the concepts of the thread pool in Node Js, one might understand that Node Js is not really single-threaded, but instead, it is also multi-threaded under the hood thanks to the thread pool.
## Summary
To summarize, Node Js is referred to as single-threaded in case of performing non-CPU intensive tasks like fetching some data from a database, and multi-threaded in case of CPU intensive tasks when using thread pool(also known as worker threads) such as complex scientific calculations or cryptographic operations. Example Node Js APIs that use the thread pool are Zlib, crypto, fs(filesystem) and DNS.

Node uses the event loop to handle those non-CPU intensive tasks and thread pool for tasks that take a long time to finish.

All incoming requests and outgoing responses pass through the event loop (main loop), so it’s advised not to block the event loop because if it spends long at any point, all other clients requests won’t get served. This means that every callback in your app should complete as quickly as possible to give other clients a turn.
What is a JavaScript Multidimensional Array?
Tags: javascript, latestposts, arrays, javascriptmultidime
Published: 2020-01-24 00:44:36

---
title: What is a JavaScript Multidimensional Array?
published: true
date: 2020-01-23 21:25:46 UTC
tags: JavaScript,Latest Posts,arrays,JavaScript multidime
canonical_url: http://robdoylecreative.com/what-is-a-javascript-multidimensional-array/
---

JavaScript Multidimensional arrays are not supported natively with a specific syntax but they can still be created by placing arrays within arrays. This means that the elements within an array consist of more arrays. The code example below shows how a JavaScript multidimensional array can be constructed to hold questions and answers for a quiz. […]

The post [What is a JavaScript Multidimensional Array?](http://robdoylecreative.com/what-is-a-javascript-multidimensional-array/) appeared first on [Rob Doyle Creative](http://robdoylecreative.com).
HTML Grid Columns with Input elements
Published: 2020-01-23 21:04:56

Assume we need a Label and an Input element like this:

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/luxcmpgnhdu8uc9rohdu.PNG)

The label content is "Label" and the input element value is "This is the default text value".  These two elements are both contained in one container (e.g. a div element) with this styling:

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/sryo1toqgwd01m4246kn.PNG)

We notice that the text content is not auto-expanding to show all text, when we change the 2nd column fr units of the grid. The text remains cut-off.

**Solution**

Because the input component is in column 2 of the grid, it's width needs to be set to 100% of that column in order to follow grid column fr changes.

Indeed we can now control the input width based only on the grid fr units as shown here:

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/o8basxilg86an0hafhwd.PNG)

The change to the fr units was this:

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/589yojzuysf6re44pcpm.PNG)

Now we see that the input element width is 7 times wider than the first column containing the label element. All made possible by a width of 100% on the input element.

**Take Away**

The grid cells themselves act as a container. Widths and Heights are relative to the cell. The limits of the cell can be overrun using numeric widths and heights, but using percentages keeps the element in the cell bounds.

This post was specific to an input element but any element in any columns needs to be 100% of that column if we want the fr units to control the width or height in a 1 to 1 ratio. No margins or padding are needed if we chose the fr units wisely.

JWP2020
Two useful Higher Order Functions(in ruby language)
Published: 2020-01-23 21:00:51

In order to talk about higher order functions and how to use them we should first understand what a higher order function is. This blog will go over briefly what a higher order function actually is, reasons they can be very useful and a couple highly used ones and how to use them. Hopefully by the end of reading this blog, the reader will be slightly more informed on the usage and origin of not only higher order functions but functions in general. 

Higher order functions are more simple than some people might realize. They are simply functions that either take in functions as parameters,  return functions as results or both. This might be a bit difficult to wrap ones head around but it might be easier to think of using a function like any other data type. Jumping into an example might make explaining easier. 

``` ruby
array = [1, 2, 3, 4, 5]

def print_nums(a)
    a.each do |element| 
    puts element + 1
    end
end
print_nums(array)
```

In this example we are working with the "each" higher order function. What this function does is it will do some work on each element of an array. In this example we use a simple array of the numbers 1 through 5. The parameter that we have here is the letter a, and the argument we pass into the print_nums call is the array called array. What each will do is that it will pass over every element inside its passed in array and do some sort of work on it. In this case, it will put out(print, console.log) each of these numbers + 1, for every number. 


``` ruby
2
3
4
5
6
=> [1, 2, 3, 4, 5]
   
```

This is our output. If we notice here each prints out each elements value + 1. BUT its return value is the original array. This is because using each we do not mutate the original array. However, as with most if not all non mutable functions we can simply set a container variable and hold what work is being done behind the scenes and just return that. 

``` ruby
array = [1, 2, 3, 4, 5]

def print_nums(a)
new_array = [] #container
    a.each do |element| 
    new_array << element + 1 #push into new array
    end
    new_array #return 
end
print_nums(array)
```
This will give us: 

``` ruby
=> [2, 3, 4, 5, 6]

```
If you were not aware, the => means return value, otherwise it is merely a log or a print.


So thats cool, but there is actually a better way to do this particular example. A useful higher order function that works with arrays is one called "map". Map will go through each element in the array, do the work, and then return a new array automatically with the work done. 

``` ruby

array = [1, 2, 3, 4, 5]

def print_nums(a)
    a.map do |element| element + 1
    end
end
print_nums(array)

```

Output: 

``` ruby
=> [2, 3, 4, 5, 6]

```
These are highly used iterators. Which for anyone who is possibly unaware just means to do something repeatedly. Iterators, of course, are extremely useful with arrays. There are many useful higher order functions but anyone in the programming world will undoubtedly be using these at some point in their careers.
Internazionalization best practices
Tags: help, internazionalization
Published: 2020-01-23 20:43:34

Hello,

I'm building an app based on *Vue* which consumes data from a *Spring Boot* based API. I have internazionalized the front-end with Vue, without issues, using a resource file.
Some of the API calls should return some localized content, based on the user-selected locale. 
I can easily make the API to return different transalations, but that means that I have to maintain two sets of translated content: one for the front-end and one for the back-end.

Is there a better approach to this (keep all the translations in one place)? Also, what would be the best way to do this? I'm thinking of passing the user locale to each API call, setting the language in the HEADER (`Accept-Language`). 

Thanks!
Luciano
Grayscale Color Palette with Equal Contrast Ratios
Tags: css, design, productivity
Published: 2020-01-23 20:41:49

---
title: Grayscale Color Palette with Equal Contrast Ratios
published: true
description: A list of grays that have the same contrast ratio against each other.
tags: css, design, productivity
cover_image: https://thepracticaldev.s3.amazonaws.com/i/iwb8xu872ivcx8vguttl.png
---

Ever needed a grayscale color palette for different surfaces, shadows, or states in your web app? I created a little utility to generate such palette.

**tl;dr**: *Scroll to the end to get the list of colors*

# But what's the big deal?

I'm glad you asked. I could have just simply created a palette that started from black `#000000`, and stepped through to white `#ffffff` with let's say increments of `#080808`. So something like (remember hexadecimal):

* `#000000`
* `#080808`
* `#101010`
* `#181818`
* ...
* `#ffffff`

But this feels a bit off, mostly because we humans don't perceive colors as hexadecimal representations. Qualities like **hue**, **saturation** and **lightness** are much better interpretations of human color perception. When you compare two different gray colors against each other they will only differ in lightness. A common method to measure difference in lightness is calculating the **contrast ratio**.

# So how is color contrast ratio calculated?

The [WCAG contrast ratio definition](https://www.w3.org/TR/WCAG21/#dfn-contrast-ratio) says:

> **contrast ratio**
>(L1 + 0.05) / (L2 + 0.05), where
>
> * L1 is the relative luminance of the lighter of the colors, and
> * L2 is the relative luminance of the darker of the colors.
>
> NOTE
> Contrast ratios can range from 1 to 21 (commonly written 1:1 to 21:1).

Actually, I already wrote an in-depth article about calculating the color contrast ratio, see below if you're interested:

{% link https://dev.to/finnhvman/which-colors-look-good-on-black-and-white-2pe6 %}

All in all, the method to calculate the contrast ratio was already given.

# Creating an equi-contrast color palette

My goal was to create a gray color palette on which each color against its adjacent color has the same contrast ratio. To generate such palette I only needed to step through the gray RGB space and store the colors that had enough contrast against the previous palette color. I picked the default contrast value of `1.10362` (that is roughly `1.1`) so that the palette spans from `#000000` to exactly `#ffffff` and consists of 30 individual colors. You can see the result in the embedded CodePen below, but feel free to play around by opening [the Pen in Edit mode](https://codepen.io/finnhvman/pen/QWwYYwJ) and modifying the value of `CONTRAST_RATIO` in the first line of JS 🙂
{% codepen https://codepen.io/finnhvman/pen/QWwYYwJ %}
#Day4 of #100DaysOfCode: Protect your eyes!
Tags: 100daysofcode, javascript, react, health
Published: 2020-01-23 20:33:27

I feel that eye health is a subject that is not talked enough in the dev community. After spending more than half of my life in front of computers, I feel that this is finally taking a toll on my eyes. After stumbling upon this amazing Dev article: [The Definitive Guide To Protecting Your Eyes For Developers](https://dev.to/sarthology/the-definitive-guide-to-protecting-your-eyes-for-developers-22jm), which features some great advice, I decided it's time to invest in my eye health. 👁👁

Here are a few steps that I've recently introduced into my daily routine:
- drinking more water
- washing my eyes more often and massaging them
- eating more fish and taking an Omega-3 supplement
- activating Night Shift on my Mac
- looking away from the computer every 20 minutes or so
- limiting my computer time when it's possible
- choosing night mode on websites where it's available

👀👀👀👀👀👀👀👀👀👀👀👀👀👀👀👀👀👀👀👀👀👀👀👀👀👀👀👀👀👀👀


Back to JavaScript, today I updated my BAT Price app to include the BAT-USD price for one hour ago. For those of you that don't know what I'm talking about, BAT is a new cryptocurrency offered as a reward for watching ads on the Brave browser. Being concerned about Google's recent privacy record, I decided to switch from Google Chrome to Brave. The small React app that I developed shows the current BAT-USD price and it updates it every 30 seconds. It will also draw a line graph to help you keep track of the price change. My reason for building this was getting into [Chart.js](https://www.chartjs.org/) and I have to say I really enjoyed working with it. 

You can try the app [here](https://gyuluu.github.io/bat-price/) - mobile friendly 📈
If you want to have a look at the code, you can find it [here](https://github.com/gyuluu/bat-price)

![BAT Price](https://thepracticaldev.s3.amazonaws.com/i/x5yj2o5chc3d8b50eidz.png)

Another takeaway of this project is that I learnt how to deploy a React app to Github Pages. If you're also wondering how, this article explains it perfectly:

[How to deploy a create react app to Github pages](https://reactgo.com/deploy-react-app-github-pages/)

**JavaScript Tip of the Day**: Math.round() - returns the value of a number rounded to the nearest integer

Example:

```javascript
console.log(Math.round(2020.1));
// expected output: 2020
console.log(Math.round(2020.8));
// expected output: 2021
```

That's about it for today. Until tomorrow, happy coding!

✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨
Trivia and Facts
Tags: discuss, watercooler
Published: 2020-01-23 20:06:48

Tell me something interesting about code and programming.

 - things we need to know but don't
 - odd things that happen
 - cool things
 - people who did important things
 - pop culture things
 - movie things

### Examples
 - @Ben asked a similar [question](https://dev.to/ben/tell-me-some-useless-or-useful-software-trivia-16hp) last year
 - Here's [what I have](https://github.com/VickiLanger/code-questions-bot/blob/master/questions.py) so far

---
#####_Full disclosure_
I'm building a [Twitter bot](https://twitter.com/CodeQBot 'CodeQBot on Twitter, please follow') to tweet questions about code. The goal is to give everyone, newbies to experts, a place to meet other devs while learning and helping each other learn. 

I would love to use your facts with the bot. If I can get it formed into a question, I will add it and give credit where credit is due.
A tool for building intuitive code documentation
Tags: webdev, productivity, startup, discuss
Published: 2020-01-23 20:03:32

Hi dev.to,

We're Rishabh, Aashish, Noah, and Moksh, the creators of Codeflow: a tool for building intuitive documentation for code. You can check it out here:

[https://codeflow.now.sh/](https://codeflow.now.sh/)

As current college students, we were all lucky enough to spend our summers interning a variety of places. One common theme we found between our experiences is it was often difficult to start working on a project and typically took many rounds of discussions to understand the flow of the code. Tools like Dropbox Paper and Confluence are great at communicating high-level specifications for engineers, but fail to capture nuances needed for new engineers to continue developing.

Codeflow is our attempt at a solution. It allows engineers to easily build presentation-esque tutorials which focus on stepping through the code logically. We hope it can become a centralized repository of tutorials designed to capture an engineer's thought process and make ramp up more efficient.

Here's an example of a tutorial built using Codeflow:

[https://codeflow.now.sh/tutorials/view/RishabhJain96/next.js/tree/b595dd/ck5q9b79d00000vlbn7ichp7p](https://codeflow.now.sh/tutorials/view/RishabhJain96/next.js/tree/b595dd/ck5q9b79d00000vlbn7ichp7p)

At this stage, we're hoping for feedback as we try to validate this approach. Any and all thoughts are appreciated.

Thanks,

Rishabh, Aashish, Noah, and Moksh
Laravel 6 Middleware Tutorial With Example
Tags: laravel, middleware
Published: 2020-01-23 19:42:10

Hello Artisan 

in this tutorial i am going to discuss about laravel middleware. In this laravel middleware tutorial we will see how we can create our custom middleware and how we can use it in our controller. 

You can see default auth middleware in laravel 6. auth middleware will protect your route url, allow only logged in user to access authorize page only. Do you know how it works. Today, i will share with you how to create custom middleware in laravel 6 application. i write step by step tutorial of use of middleware in php laravel 6.

https://www.codechief.org/article/laravel-6-middleware-tutorial-with-example
A Greedy Algorithm for Job Sequencing with Deadlines and Profits
Tags: greedy, algorithms, jobsequencing, deadlines
Published: 2020-01-23 20:02:25

In this chapter we will see greedy algorithm examples. In this tutorial we will learn about Job Sequencing Problem with Deadline. This problem consists of n jobs each associated with a deadline and profit and our objective is to earn maximum profit.

We will earn profit only when job is completed on or before deadline. We assume that each job will take unit time to complete.

https://www.codechief.org/article/a-greedy-algorithm-for-job-sequencing-with-deadlines-and-pro-fits
Divide and Conquer Algorithms with Source Code
Tags: divide, conquer, algorithms, sourcecode
Published: 2020-01-23 19:59:37

Divide and conquer is an algorithm design paradigm based on multi-branched recursion.

A divide and conquer algorithm works by recursively breaking down a problem into two or more sub-problems of the same type, until these become simple enough to be solved directly. The solutions to the sub-problems are then combined to give a solution to the original problem

https://www.codechief.org/article/divide-and-conquer-algorithms-with-source-code
Fractional Knapsack Source Code using C++
Tags: fractional, knapsack, source, code
Published: 2020-01-23 19:58:08

This is a C++ program to find Fractional Knapsack.This C++ program finds Fractional Knapsack. Program Here is the source code of the C++ program to find Fractional Knapsack.

https://www.codechief.org/article/fractional-knapsack-source-code-using-c
0/1 Knapsack Using Dynamic Programming Approach with Source Code
Tags: knapsack, dynamic, programming, greedy
Published: 2020-01-23 19:57:22

The knapsack problem or rucksack problem is a problem in combinatorial optimization: Given a set of items, each with a weight and a value, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible.

It derives its name from the problem faced by someone who is constrained by a fixed-size knapsack and must fill it with the most valuable items.

https://www.codechief.org/article/01-knapsack-using-dynamic-programming-approach
Domain records for dummies
Tags: beginners, domain, dns
Published: 2020-01-23 19:50:07

---
title: Domain records for dummies
published: true
description: 
tags: beginners,domain,dns
---

If you own a domain name, you get to choose what site is displayed on it. But there's more to that: it's also possible to determine who can **use the domain to send emails or you can verify your ownership**. How does this work?

## First things first: *nameservers*

By setting the nameservers, you basically tell the domain what rules to listen to. By default, these will probably be the servers from the provider you bought the domain from. However, you can pick any nameservers you want.

## Getting your settings right: *DNS records*

These nameservers can set DNS records to specify all sorts of stuff.

### A record
Tell the domain what server (by IP) to use for the website. Most of the time, this one will be the most important.

### AAAA record
Using an IPv6 for your server? Then you'd need to use an AAAA record instead of a regular A record.

### CNAME record
Defines an "alias" and thus points to another domain that, in turn, might have an A record.

### MX record
A Mail Exchange record will make sure the correct mail server is used.

### TXT record
A TXT record can basically be anything. They are often used to confirm ownership of the domain, which is useful for some Google services.

### SRV record
Used to connect to other services. Might be used for email of other software. These aren't used too much, in my experience.

## Help! I set some records but they won't work

### Using the right nameservers?
It's possible to have records on other nameservers than the active one.
Domain name >> nameservers (service B) >> records (service B)
If you are editing records on service A, they won't take into effect until the nameservers point to service A.

### Account for DNS caching
DNS settings are cached on several levels, so it might take a while before you see a difference. In general, it's best to account for 24h when changing nameservers and up to 4h for changes in the records.

> Tip: if you know a change is coming, lower the Time To Live (TTL) setting beforehand. The default of 3600 seconds means a record will be updated once every hour.

Check the active settings with tools like [Google dig](https://toolbox.googleapps.com/apps/dig). Waited long enough and still not updated? Check on a different network of with 4G. If it's allright there, the settings might be cached on your local network.

**Anything to add? Don't hesitate to leave a comment, would love to learn from you!**