<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: aderchox</title>
    <description>The latest articles on Forem by aderchox (@aderchox).</description>
    <link>https://forem.com/aderchox</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F293331%2Fca9ef521-ad29-4684-a85b-1ae4fb617df2.jpg</url>
      <title>Forem: aderchox</title>
      <link>https://forem.com/aderchox</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/aderchox"/>
    <language>en</language>
    <item>
      <title>What is "the time"? ⏳ ...and what is meant by UTC in date.toUTCString()?</title>
      <dc:creator>aderchox</dc:creator>
      <pubDate>Wed, 31 May 2023 23:51:12 +0000</pubDate>
      <link>https://forem.com/aderchox/what-is-the-time-and-what-is-meant-by-utc-in-datetoutcstring-2b57</link>
      <guid>https://forem.com/aderchox/what-is-the-time-and-what-is-meant-by-utc-in-datetoutcstring-2b57</guid>
      <description>&lt;h2&gt;
  
  
  Background
&lt;/h2&gt;

&lt;p&gt;Pretty much in all major programming languages there's support for working with dates and times. JavaScript for instance, has a Date class for this purpose, and this class has many methods. Among all of these methods though, one is &lt;code&gt;toUTCString()&lt;/code&gt;, which I happened to have encountered a million times, and yet had no clear understanding of it (well, of course, before Googling and writing this article).&lt;/p&gt;

&lt;p&gt;Checking its documentation on the MDN first, we'll see:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The toUTCString() method converts a date to a string, interpreting it in the UTC time zone.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Hmm... so UTC is a time zone! Just like Greenwich (GMT), so let's Google it to see how it's different from GMT:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Coordinated Universal Time or UTC is the primary time standard by which the world regulates clocks and time.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Well, but that sounds a lot like GMT!?&lt;/p&gt;

&lt;p&gt;So I Google GMT vs UTC next:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Prior to 1972, this time was called Greenwich Mean Time (GMT) but is now referred to as Coordinated Universal Time or Universal Time Coordinated (UTC).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Hmm... that's odd! It cannot be just about a "rename"!&lt;/p&gt;

&lt;h2&gt;
  
  
  What is the difference between GMT and UTC &lt;em&gt;really&lt;/em&gt;?
&lt;/h2&gt;

&lt;p&gt;Although this is not a coding question, programmers bump into it pretty often, so a non-programming and easy to understand answer to it would be worthy.&lt;/p&gt;

&lt;p&gt;In simple terms, we can assume that &lt;strong&gt;both GMT and UTC are "clocks" that tell us "the time"&lt;/strong&gt;, however, UTC is the more precise one, so it took GMT's place in our measurement of the time around the world (in 1972).&lt;/p&gt;

&lt;p&gt;But wait, &lt;strong&gt;what does "a more precise clock" mean&lt;/strong&gt; at all? To understand this, we need to understand what "the time" itself really is in the first place!&lt;/p&gt;

&lt;h2&gt;
  
  
  What is "the time? ⏳🔮
&lt;/h2&gt;

&lt;p&gt;Time (in its today's form, i.e., months, days, hours, minutes, etc.) is a human-made thing, and is our "&lt;strong&gt;means of measuring the passage of events&lt;/strong&gt;". Say you're planning a programming event next month. What is &lt;em&gt;next month&lt;/em&gt;? So you need to have an a shared and wide-spread understanding of the time (the amount of the passage of the events) we all consider "next month"!&lt;/p&gt;

&lt;p&gt;Our today's shared understanding of "the time" is the result of a convention/agreement our ancestors left for us: They searched for some regularly recurring event, and they initially found it in the change of day to night, and in the change of seasons. (If you notice, these are astronomical events, i.e., rotation of the earth around its own axis, and sun's position in the sky. This is what the GMT clock (timezone) uses to measure the time). But there was still room for improvement:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The measurement of time began with the invention of sundials in ancient Egypt some time prior to 1500 B.C. However, the time the Egyptians measured was not the same as the time today's clocks measure. For the Egyptians, and indeed for a further three millennia, &lt;strong&gt;the basic unit of time was the period of daylight&lt;/strong&gt;. &lt;a href="https://www.maa.org/external_archive/devlin/devlin_12_99.html"&gt;source&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So basically, you could tell someone "I'll see you tomorrow", but you couldn't tell them "I'll see you tomorrow at 7:30"! :D&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3gV-56IX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ws7emsff9bjyb39cr20.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3gV-56IX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ws7emsff9bjyb39cr20.gif" alt="Donald Duck Vintage Time-in Gif Image via Giphy" width="480" height="352"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So the next [obvious] step for them was to make their measurement of the time more precise, and as a result, they agreed (not surprisingly) to divide each full day into 24 parts, aka "hours", and they agreed to divide each hour into 60 minutes, and so on. So today you have something called &lt;strong&gt;"a clock" that allows you "to universally keep your understanding of every point of the past, of the current and of the future events in sync with others"&lt;/strong&gt;, therefore you can happily plan your event next month, without an issue (well, without a "temporal issue" at least!) 🕙🕜🕞.&lt;/p&gt;

&lt;p&gt;Why was UTC needed then? UTC was needed for the exact same reason, it was a further step in this interminable road towards our unquenchable thirst for more and more precision. As we've said above already, GMT used some not-stable-enough astronomical events to tell us the time, i.e., events not as precisely regular as we expect to set as the basis of our modern agreement for measuring the time.&lt;/p&gt;

&lt;p&gt;Don't get me wrong, actually, the GMT clock is still good enough for numerous purposes, but it's just like a ruler that measures in units only as small as millimeters, while we need greater length precisions as well definitely once we get into realms like engineering, science and so on.&lt;/p&gt;

&lt;p&gt;Here's where UTC comes in wonderfully. UTC measures "frequency of an atom's vibration" (*), and that happens to be a much more stable and regularly recurring event compared to things like earth's rotations or sun's position in the sky. Indeed, we're talking about 1 billionths of a second per day v.s up to a few minutes per day! So no wonder UTC can keep us in sync in a much more reliable way.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1qZOymfF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u4bqsmm52x3qriy441lt.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1qZOymfF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u4bqsmm52x3qriy441lt.gif" alt="High Five Bro with Cat Gif - via giphy" width="350" height="282"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's it, ...for the most part! If you're a curious mind, there's still one more delicate (and important) point we've missed to mention here. If you think about it, UTC is great in terms of its precise measuring of the time, however, it also creates a (minor) issue: &lt;strong&gt;We actually do care about what a full day is! This is regardless of how we measure the time!&lt;/strong&gt; No matter how accurate a clock is, no matter how precisely it keeps us in sync to each other, we do not like it to make our days dark and to make our nights bright! (with a bit of exaggeration). So we need to occasionally adjust the UTC clock, and this is exactly what we do, using a second aka a "leap second" to . From &lt;a href="https://en.wikipedia.org/wiki/Leap_second"&gt;Wikipedia&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A leap second is a one-second adjustment that is occasionally applied to Coordinated Universal Time (UTC), to accommodate the difference between precise time (International Atomic Time (TAI), as measured by atomic clocks) and imprecise observed solar time (UT1), which varies due to irregularities and long-term slowdown in the Earth's rotation.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt; - (*) Disclaimer: I'm neither a Physicist nor an Astronomer, and these topics are very complicated, so this answer should have some inaccuracies naturally. If you need a deeper and more accurate understanding, read further in &lt;a href="https://physics.stackexchange.com/a/440607/281658"&gt;this answer&lt;/a&gt; on Physics Stack Exchange.&lt;br&gt;
 - Cover Image from Unsplash by &lt;a href="https://unsplash.com/photos/UAvYasdkzq8"&gt;Lukas Blazek&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>tutorial</category>
      <category>devjournal</category>
      <category>learning</category>
    </item>
    <item>
      <title>What is the DEBUG 🐛 environment variable in Node.js, and how to use it?</title>
      <dc:creator>aderchox</dc:creator>
      <pubDate>Tue, 04 Apr 2023 13:44:51 +0000</pubDate>
      <link>https://forem.com/aderchox/what-is-the-debug-environment-variable-in-nodejs-and-how-to-use-it-3fal</link>
      <guid>https://forem.com/aderchox/what-is-the-debug-environment-variable-in-nodejs-and-how-to-use-it-3fal</guid>
      <description>&lt;p&gt;(The credit for the cover image goes to &lt;a href="https://coralogix.com"&gt;coralogix&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;You might have seen a command like this while navigating in Node.js projects:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ DEBUG=myapp:* npm start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For example, in Express, you might see:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DEBUG=express:* node index.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So what is it?&lt;/p&gt;

&lt;p&gt;Although it's used by Express, it's indeed more broadly, the way a popular NPM package called &lt;a href="https://www.npmjs.com/package/debug"&gt;debug&lt;/a&gt; works, which is &lt;a href="https://github.com/expressjs/express/blob/0debedf4f31bb20203da0534719b9b10d6ac9a29/package.json#L38"&gt;used&lt;/a&gt; internally in Express too. Under the hood, the debug package expects the &lt;code&gt;DEBUG&lt;/code&gt; environment variable to determine &lt;em&gt;what debug messages&lt;/em&gt; to print (could be on the console, or into a file, or into stdout to be &lt;a href="https://www.coreycleary.me/why-should-your-node-js-application-not-handle-log-routing"&gt;collected&lt;/a&gt; by a log aggregator service).&lt;/p&gt;

&lt;p&gt;In case of Express specifically, to see all the internal logs used in it, we &lt;a href="https://expressjs.com/en/guide/debugging.html"&gt;could&lt;/a&gt; set the &lt;code&gt;DEBUG&lt;/code&gt; environment variable to &lt;code&gt;express:*&lt;/code&gt; when launching the app. E.g., on Linux, run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DEBUG=express:* node index.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or to see logs only from the application implementation, run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DEBUG=express:application node index.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The syntax used in both of the above commands (&lt;code&gt;FOO=bar command&lt;/code&gt;) is the general Linux-y way of defining a one-off environment variable exclusive to only a single execution of a command, i.e., will only be accessible to the &lt;code&gt;command&lt;/code&gt; and only at that single time. It's very beneficial that it's a one-off thing, as it won't pollute the environment variables' global space unnecessarily).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;debug is like an augmented version of console.log, but &lt;strong&gt;unlike console.log, we don’t have to comment out debug logs in production code. Instead, logging is turned off by default and can be conditionally turned on by using the DEBUG environment variable.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You can also specify more than one debug namespace (whether regarding Express, or regarding any other modules by assigning a comma-separated list of names:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DEBUG=http,mail,express:* node index.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you wonder what a "namespace" is, it's again related to the debug package, for example, consider this snippet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;debug&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;debug&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;httpServerLog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;debug&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http:server&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What is that 'http:server'? It's simply the flag/namespace for that specific logger that you will pass to the DEBUG environment variable in order to turn ON the debugging. So now you could do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;http&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createServer&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;httpServerLog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;method&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;end&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;debug example&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nx"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;httpServerLog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;listening&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And to enable the above logs, the "http:server" namespace has to be enabled, so we'll do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DEBUG=http:server node app.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You could check the package.json of any 3rd-party packages to see if they've used the debug module or not, and if they have, you could check the namespaces they've defined and pass them using the same method (of passing the &lt;code&gt;DEBUG&lt;/code&gt; environment variable to pass the desired namespaces) to see their logs.&lt;/p&gt;

&lt;p&gt;Read more about the "debug" package &lt;a href="https://www.npmjs.com/package/debug"&gt;here&lt;/a&gt; and &lt;a href="https://www.coreycleary.me/using-the-debug-module-to-avoid-polluting-your-application-logs-with-logs-from-node-modules"&gt;here&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;Hey, you're also invited to join our small and fledging 🐥🐣 Discord community called &lt;strong&gt;TIL&lt;/strong&gt; (stands for "&lt;strong&gt;Today-I-Learned&lt;/strong&gt;") where we share things that we learn with each other, in the realm of web programming, using this &lt;a href="https://discord.gg/XxzKSgdSAD"&gt;link&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Thanks for reading this article 👋 You're very welcome to suggest any fixes or improvements in the comments section below.&lt;/p&gt;

</description>
      <category>node</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Create a pure Node.js and TypeScript app + Getting node-fetch ESM-only (version 3) up and running</title>
      <dc:creator>aderchox</dc:creator>
      <pubDate>Thu, 16 Mar 2023 19:22:30 +0000</pubDate>
      <link>https://forem.com/aderchox/create-a-pure-nodejs-and-typescript-app-getting-node-fetch-esm-only-version-3-up-and-running-31jh</link>
      <guid>https://forem.com/aderchox/create-a-pure-nodejs-and-typescript-app-getting-node-fetch-esm-only-version-3-up-and-running-31jh</guid>
      <description>&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;Node.js is a browserless (server-only) runtime environment for JavaScript apps, node-fetch gives Node.js apps an API similar to the browsers' standard &lt;code&gt;fetch()&lt;/code&gt; API, and TypeScript provides static typing which is cool. In this article, we're going to bootstrap a project that glues these three together.&lt;/p&gt;

&lt;h3&gt;
  
  
  node-fetch version 3 is ESM-only
&lt;/h3&gt;

&lt;p&gt;Node.js apps used a module system called CommonJS in the past (modules in this system are imported like this: &lt;code&gt;const x = require("/path/to/module");&lt;/code&gt;). ESM system wasn't a thing yet then, but since its emerge (first in browser environments), Node.js has also started the transition (which has some benefits compared to CommonJS system, and its imports are like this: &lt;code&gt;import x from "/x.js"&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Since the release of Node.js version 14 in April 2020, it has had stable support for the ESM module system. So no wonders package authors have also tried their best to keep up to speed with this transition and have provided ESM support for their packages too.&lt;/p&gt;

&lt;p&gt;Usually, package authors support both these module systems (despite the difficulties of doing it, e.g., automating, maintaining, bugs, etc.) for the sake of backwards compatibility, however, among all packages, there are also ones whose authors have pulled the future close to "now" faster..., and are offering "ESM only". fetch-node version 3 is an example of these &lt;em&gt;&lt;strong&gt;ESM-only&lt;/strong&gt;&lt;/em&gt; packages.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Steps
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;1. Create a directory for the project, cd into it, initiate a Node.js project, and install TypeScript and node-fetch packages:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir ts-node-fetch-app
cd ts-node-fetch-app
npm init -y
npm i typescript node-fetch@3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;2. Create &lt;code&gt;app.ts&lt;/code&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;fetch&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;node-fetch&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Todo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;completed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;fetchTodo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s2"&gt;`https://jsonplaceholder.typicode.com/todos/&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;todo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;todo&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Todo&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;todoId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nf"&gt;fetchTodo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;todoId&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;todo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;todo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;NOTICE: In a real project, you'd commonly have a &lt;code&gt;src&lt;/code&gt; directory for your source files and a &lt;code&gt;build&lt;/code&gt; directory for your compiled modules and so on. This is a quick project however, for demonstration purposes only. So we've kept things utterly minimal and [hopefully] on-point.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;3. Generate a configuration file for TypeScript (tsconfig.json):
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx tsc --init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we should have a &lt;code&gt;tsconfig.json&lt;/code&gt; file with some default options in it. We need to change the value of the option &lt;code&gt;module&lt;/code&gt; though, so that the module code generated by the TypeScript compiler will work on Node.js. We'll change its value from "commonjs" to "node16".&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;NOTICE: We could use "nodenext" too, but "node16" suffices for our purposes. You may also put the cursor in the middle of the double quotations and hit CTRL+SPACE to get intellisense on the available values for each of the options.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;After making the above change and removing all the comments, this is our final &lt;code&gt;tsconfig.json&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"compilerOptions"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"target"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"ES2016"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"module"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Node16"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"esModuleInterop"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"forceConsistentCasingInFileNames"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; 
    &lt;/span&gt;&lt;span class="nl"&gt;"strict"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"skipLibCheck"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;4. Since node-fetch version 3 is ESM-only, we have to ESM module system in all our Node.js project. Node.js, however, by default assumes all projects to be "commonjs", unless otherwise specified using in the &lt;code&gt;package.json&lt;/code&gt;. So in the &lt;code&gt;package.json&lt;/code&gt; add:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"module"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;5. Now compile the &lt;code&gt;app.ts&lt;/code&gt; to JavaScript:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx tsc
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;6. And run it:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;node app.js // Prints: delectus aut autem
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx8nulcb3fk7ivucjb4bp.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx8nulcb3fk7ivucjb4bp.gif" alt="Funny GIF titled Give Me More" width="480" height="233"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Bonuses
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;7. We can also use top-level await in our &lt;code&gt;app.ts&lt;/code&gt; instead of the chaining &lt;code&gt;.then()&lt;/code&gt; syntax used. Top-level 'await' expressions 
though are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', or 'nodenext', and the 'target' option is set to 'es2017' or higher. Our &lt;code&gt;tsconfig.json&lt;/code&gt; is fine for the most part, except for the target option which is on "es2016". Let's tweak it quickly and change it to "ES2022":
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"compilerOptions"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"target"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"ES2022"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"module"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Node16"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"esModuleInterop"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"forceConsistentCasingInFileNames"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; 
    &lt;/span&gt;&lt;span class="nl"&gt;"strict"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"skipLibCheck"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now &lt;code&gt;app.ts&lt;/code&gt; may be re-written with top-level await in it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;fetch&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;node-fetch&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Todo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;completed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;fetchTodo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s2"&gt;`https://jsonplaceholder.typicode.com/todos/&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;todo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;todo&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Todo&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;todoId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// Top-level await:&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;todo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetchTodo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;todoId&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Todo Title: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;todo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now compile and run it with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx tsc
node app.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;8. Currently, every time we make a change, we have to manually re-compile and re-execute our project. This is not cool. So let's use a tool that can do these both automatically for us. I'll use tsx. Check out their &lt;a href="https://github.com/esbuild-kit/tsx" rel="noopener noreferrer"&gt;repository&lt;/a&gt; and also don't forget to leave them a star if you find it useful.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;NOTICE: Obviously you can use any other packages too, but make sure to check their ESM support beforehand. For instance, at the time of writing this article, &lt;a href="https://github.com/wclr/ts-node-dev" rel="noopener noreferrer"&gt;ts-node-dev&lt;/a&gt;'s ESM support seems to be &lt;a href="https://github.com/wclr/ts-node-dev/issues/314#issuecomment-1445231219" rel="noopener noreferrer"&gt;missing&lt;/a&gt; support ESM yet.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i -D tsx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;NOTICE: We'll use it in development only, so I've added a &lt;code&gt;-D&lt;/code&gt; option to specify it as a devDependency in the &lt;code&gt;package.json&lt;/code&gt; and later on we'll be able to prevent its installation once not needed anymore, e.g., using &lt;code&gt;npm install --omit=dev&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now add this script in the &lt;code&gt;package.json&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"dev"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"tsx watch --tsconfig ./tsconfig.json ./app.ts"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now run the project with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;9. We may also use types similar to the types defined in &lt;a href="https://github.com/microsoft/TypeScript/blob/main/src/lib/webworker.generated.d.ts#L6648" rel="noopener noreferrer"&gt;&lt;code&gt;microsoft/TypeScript&lt;/code&gt;&lt;/a&gt; for the standard &lt;code&gt;fetch()&lt;/code&gt; API, and define a custom fetcher function that is typed generically.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;NOTICE: An easy way to find these standard types is by typing them in the editor and ctrl-clicking on them.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We've called this generically typed function "fetchData". Here's the final &lt;code&gt;app.ts&lt;/code&gt; code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;RequestInfo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;RequestInit&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;node-fetch&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Todo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;completed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;RequestInfo&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;URL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;fetchData&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;init&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;RequestInit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;init&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;todo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;todo&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;fetchTodo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;fetchData&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Todo&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s2"&gt;`https://jsonplaceholder.typicode.com/todos/&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;todoId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;todo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetchTodo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;todoId&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Todo Title: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;todo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Hey, you're invited to join our small and fledging 🐥🐣 Discord community called &lt;strong&gt;TIL&lt;/strong&gt; (stands for "&lt;strong&gt;Today-I-Learned&lt;/strong&gt;") where we share things that we learn with each other, in the realm of web programming. Join using this &lt;a href="https://discord.gg/XxzKSgdSAD" rel="noopener noreferrer"&gt;link&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Thanks for reading this article 👋 You're also very welcome to suggest any fixes and improvements in the comments section below.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>node</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Let's learn Solid.js quickly, by creating a useDebounce hook</title>
      <dc:creator>aderchox</dc:creator>
      <pubDate>Thu, 02 Mar 2023 15:26:08 +0000</pubDate>
      <link>https://forem.com/aderchox/lets-learn-solidjs-quickly-by-creating-a-usedebounce-hook-3hf0</link>
      <guid>https://forem.com/aderchox/lets-learn-solidjs-quickly-by-creating-a-usedebounce-hook-3hf0</guid>
      <description>&lt;h4&gt;
  
  
  Disclaimer: I'm not a Solid.js affiliate in any ways, and also I'm not experienced enough with Solid.js (~2days!) to claim what I write here is "good solid.js code" or even "correct".
&lt;/h4&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Although I started learning &lt;strong&gt;Solid.js&lt;/strong&gt; just two days ago, I feel it's made me very productive. One thing I tried to do with it as a  first personal exercise, was creating a "drawer" (drawers are those menus that enter the page from one side, in case you didn't know).&lt;/p&gt;

&lt;p&gt;I wrote just a few lines of very understandable (read "lovely") code, and then voila, it worked!&lt;/p&gt;

&lt;p&gt;At this point, I felt very happy with the journey I'd begun, so I decided to do a second exercise: &lt;strong&gt;To make the drawer "less crazy"! I.e., I didn't want it to expand "just when the mouse started hovering on it", I wanted it to "wait" for some time (a delay) and let the user make up their mind whether they actually wanted the drawer to open or not. So I needed a debouncer!&lt;/strong&gt; (Don't worry if you don't know what it is, I'll explain).&lt;/p&gt;

&lt;p&gt;It happened to be an interesting exercise in the end, and it taught me more of Solid's API, so I decided to share it with other peer learners (you!) too. Don't worry if you don't know any Solid.js, I'll try to explain things bit by bit.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Debouncing?
&lt;/h2&gt;

&lt;p&gt;To me, debouncing means &lt;strong&gt;"not jumping into conclusions"&lt;/strong&gt;. A practical example of when a debouncer can be useful is, when the user is typing letters into a search box, and you want to "trigger the search as the user types", you think this would be great UX, the user doesn't have to click/tap the magnifier (search) button, however, there's an issue with this great idea, EVERY letter that the user types, will make a call to your backend..., which is wasteful. Actually, it could even be "bad UX", assume the search box reacting to every letter instantly as the user types, they might feel the search box is made by a crazy developer!&lt;/p&gt;

&lt;h2&gt;
  
  
  Solid.js Quick Start Tutorial
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Reactivity
&lt;/h3&gt;

&lt;p&gt;The general idea of UI frameworks is to make views react automatically to changes to "the state of the data" (aka, just "the state"). This makes it way easier to write and reason about UI code, compared to using a bunch of event listeners here and there to "manually" update views once the data has changed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Signals
&lt;/h3&gt;

&lt;p&gt;Solid uses the term "signal" for state (data). Well of course that might not be a precise definition of signals, but it's good enough for the beginning. Let's continue the talk in code (which is not cheap, hopefully!):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Likes&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;likeCount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setLikeCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createSignal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt;
      &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;like&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;setLikeCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;likeCount&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}}&lt;/span&gt;
    &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nf"&gt;likeCount&lt;/span&gt;&lt;span class="p"&gt;()}&lt;/span&gt; &lt;span class="nx"&gt;likes&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(playground: &lt;a href="https://playground.solidjs.com/anonymous/20a27607-7aed-4c34-9249-e09580131742" rel="noopener noreferrer"&gt;https://playground.solidjs.com/anonymous/20a27607-7aed-4c34-9249-e09580131742&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;In the example above:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;This function is a Solid "component", we've called it "Likes" component. Solid knows how to translate this function into a component under the hood. We just need to return some html from the function (which is basically not "normal HTML", it's "JSX", which gets translated to JavaScript by a compiler before creating the DOM, but we don't need to care about that).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;(Read this if you're coming from React) You don't need to worry about stale closures when using signal setters in Solid, i.e., passing a mapper function (mapping old value to new value) to the &lt;code&gt;setLikeCount()&lt;/code&gt; instead of passing the actual value wouldn't be considered a best practice, you &lt;em&gt;could&lt;/em&gt; do that, but it's not different. This is due to the different model that Solid has towards reactivity, it doesn't re-run the entire component on re-renders, rather, only the points that need to change, do change &lt;em&gt;atomically&lt;/em&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We can make "signals" for our data using the &lt;code&gt;createSignal&lt;/code&gt; function, and then use the signals in our JSX, by "calling" them (e.g., &lt;code&gt;{ likeCount() }&lt;/code&gt; &lt;sup&gt;Note: it has to be wrapped in curly braces due to how JSX works.&lt;/sup&gt;). This will make our view "reactive" to the &lt;code&gt;likeCount&lt;/code&gt;, and whenever this datum is changed, "all things" that have used it (with a call) will also get updated.&lt;br&gt;
I said all "things" on purpose, because the dependent doesn't have to be only JSX, it can even be another JavaScript variable, or an effect (more on this a bit later). For example, let's say we want the background color of the button to go green when the number of likes reaches 3:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;createSignal&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;solid-js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;render&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;solid-js/web&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Likes&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;likeCount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setLikeCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createSignal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;likesColor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;likeCount&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;limegreen&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;-- The value of the likesColor depends on the value of the likeCount signal, and gets updated whenever likeCount changes. likesColor is also known as a "derived signal".&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt;
      &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;like&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;setLikeCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;likeCount&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}}&lt;/span&gt;
      &lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{{&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;background-color&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;likesColor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;}}&lt;/span&gt;
    &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nf"&gt;likeCount&lt;/span&gt;&lt;span class="p"&gt;()}&lt;/span&gt; &lt;span class="nx"&gt;likes&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Likes&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;app&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(playground: &lt;a href="https://playground.solidjs.com/anonymous/3bfab2a8-14cf-46e2-873e-2b14adf5b48b" rel="noopener noreferrer"&gt;https://playground.solidjs.com/anonymous/3bfab2a8-14cf-46e2-873e-2b14adf5b48b&lt;/a&gt;)&lt;/p&gt;

&lt;h3&gt;
  
  
  Effects
&lt;/h3&gt;

&lt;p&gt;Now let's say, whenever the Like button is clicked, we want to shout it out to the world! For such a thing, we need to "track the signal", which is called an "effect".&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;createEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`I Was Liked &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nf"&gt;likeCount&lt;/span&gt;&lt;span class="p"&gt;()}&lt;/span&gt;&lt;span class="s2"&gt; times, World!`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(playground: &lt;a href="https://playground.solidjs.com/anonymous/afaae217-4c65-45e2-a88b-a1f0fb666053" rel="noopener noreferrer"&gt;https://playground.solidjs.com/anonymous/afaae217-4c65-45e2-a88b-a1f0fb666053&lt;/a&gt;)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;(Read this if you're coming from React) No dependencies arrays have been passed to &lt;code&gt;createEffect&lt;/code&gt;, it is not a mistake. Manually passing a dependencies array to &lt;code&gt;createEffect&lt;/code&gt; is not needed (and does not exist) in Solid, because Solid signals are used in the effect by "calling them as functions", it is able to detect its own dependencies automatically. Solid also provides other APIs like &lt;code&gt;on&lt;/code&gt; for more granular control on effect's dependencies once it's needed (we'll see a forthcoming example of it soon).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Well...... our effect &lt;em&gt;works&lt;/em&gt;, but it's kind of lame! Because it even shouts on the very first render, i.e., when we haven't been liked at all!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I Was Liked 0 times, World!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F35k3ca4wryg0tanevbp9.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F35k3ca4wryg0tanevbp9.gif" alt="Funny GIF indicating, Nah, no way. A man shaking his head like it's a ball of meat." width="344" height="344"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So let's fix this &lt;em&gt;major&lt;/em&gt; issue. To fix it, we need to skip the first trigger of the effect (the function passed to the &lt;code&gt;createEffect&lt;/code&gt;), and for that, we need to wrap the effect in &lt;code&gt;on&lt;/code&gt;. This is NOT the main purpose of the &lt;code&gt;on&lt;/code&gt; function, rather, it's a means of making dependencies of the effect explicit. (Sometimes we have signals in an effect that we don't want changes to them to re-trigger the effect.) But it's not related to our use-case here, what we want to use from it is just the options object that we can pass to it which allows us to skip the first trigger:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="nf"&gt;createEffect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;likeCount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`I Was Liked &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; times, World!`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;defer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;}));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(playground: &lt;a href="https://playground.solidjs.com/anonymous/1a15305d-2620-4aad-9b7f-ee1e633c002b" rel="noopener noreferrer"&gt;https://playground.solidjs.com/anonymous/1a15305d-2620-4aad-9b7f-ee1e633c002b&lt;/a&gt;)&lt;br&gt;
As you see in the snippet above:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The first argument of the &lt;code&gt;on&lt;/code&gt; function is the signal we want our effect to explicitly depend on.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The second argument is the effect itself, however, the signal's VALUE is passed to it (not the signal itself, so we use it by &lt;code&gt;c&lt;/code&gt; and not by &lt;code&gt;c()&lt;/code&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now it works just the way we expected!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyd3ar8a75ms8f4gma6xb.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyd3ar8a75ms8f4gma6xb.gif" alt="High Five Lets Go GIF" width="372" height="372"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  The useDebounce hook
&lt;/h2&gt;

&lt;p&gt;Now back to our initial goal at hand! This is an example of how we want to use this hook:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;isOpen&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setIsOpen&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createSignal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isOpenDebounced&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useDebounce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isOpen&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// A 400 milliseconds delay to let the user make up their mind,&lt;/span&gt;
&lt;span class="c1"&gt;// or regret it (usually this value is between 400 to 600).&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's just code it up and hopefully by now, it will be talking to you in a more familiar language:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;useDebounce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;debouncedSignal&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setDebouncedSignal&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createSignal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;timerHandle&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;createEffect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;timerHandle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;setDebouncedSignal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nf"&gt;onCleanup&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;clearTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;timerHandle&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;debouncedSignal&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(playground: &lt;a href="https://playground.solidjs.com/anonymous/7c8c8d82-502b-4a31-ab96-2cb45a6e705a" rel="noopener noreferrer"&gt;https://playground.solidjs.com/anonymous/7c8c8d82-502b-4a31-ab96-2cb45a6e705a&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;In case you don't understand it yet, it's your own fault, re-read everything, just kidding. The gist of it is, we have defined an effect which depends on the signal we want to debounce, so whenever the signal is reset, the effect is re-triggered, and also &lt;code&gt;onCleanup&lt;/code&gt; will clear the previously set timer.&lt;/p&gt;

&lt;p&gt;There's a big downside to the above solution though, the passed state MUST be initiated using the &lt;code&gt;equals: false&lt;/code&gt; option, so that createEffect gets re-triggered even when the state is set with the same value!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;likeCount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setLikeCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createSignal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;equals&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Of course not ideal.&lt;/p&gt;

&lt;h2&gt;
  
  
  A &lt;em&gt;much more Solid&lt;/em&gt; solution!
&lt;/h2&gt;

&lt;p&gt;A better (&lt;strong&gt;much better&lt;/strong&gt; 😅) implementation of the useDebounce debounces the "state setter" (not the getter), e.g., the &lt;code&gt;setLikeCount&lt;/code&gt;, not the &lt;code&gt;likeCount&lt;/code&gt;. Not only this solution is easier, more readable, and doesn't require setting an &lt;code&gt;{ equals: false }&lt;/code&gt; option, but also demonstrates how close Solid.js can be to vanilla JavaScript!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;useDebounce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;signalSetter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;timerHandle&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;debouncedSignalSetter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;clearTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;timerHandle&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;timerHandle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;signalSetter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nx"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nf"&gt;onCleanup&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;clearInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;timerHandle&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;debouncedSignalSetter&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(playground: &lt;a href="https://playground.solidjs.com/anonymous/0850d269-ca93-4a6d-9607-ca8c2aa2f0cb" rel="noopener noreferrer"&gt;https://playground.solidjs.com/anonymous/0850d269-ca93-4a6d-9607-ca8c2aa2f0cb&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;&lt;code&gt;createEffect&lt;/code&gt; and &lt;code&gt;on&lt;/code&gt; are not needed anymore!&lt;/p&gt;

&lt;p&gt;In case you're wondering what happens to &lt;code&gt;clearTimeout&lt;/code&gt; in the first render: &lt;code&gt;clearTimeout&lt;/code&gt;'s API is OK with &lt;code&gt;undefined&lt;/code&gt; passed to it and just ignores it. Also in case you're wondering why we need that &lt;code&gt;onCleanup&lt;/code&gt; call, that's because the last time the timer is set, the component might unmount without cleaning up the timer, which could cause an error. (This example also shows how &lt;code&gt;onCleanup&lt;/code&gt; is not dependent on &lt;code&gt;createEffect&lt;/code&gt;, which might be against what you're used to, if you're coming from React.)&lt;/p&gt;

&lt;p&gt;This is the final result:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd8rlc09wxj17gw58kx7w.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd8rlc09wxj17gw58kx7w.gif" alt="Preview of the final result" width="330" height="202"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Bonus: Solid Primitives Library
&lt;/h2&gt;

&lt;p&gt;Although the above solution &lt;em&gt;works&lt;/em&gt;, it's not tested enough, and as I initially said, I'm not experienced enough with Solid to guarantee the quality of the above solution. But no worries, thankfully Solid's community is so helpful and they've already provided a library like React's usehooks-ts for such frequent needs, and they've called it "solid-primitives". So if you want a battle-tested solution for debouncing using Solid, I recommend using the &lt;a href="https://github.com/solidjs-community/solid-primitives/tree/main/packages/scheduled#debounce" rel="noopener noreferrer"&gt;debounce primitive&lt;/a&gt; from this library. Install, import and use it just the way we used our own &lt;code&gt;debounce&lt;/code&gt; hook above! Please don't forget to also give them a star if their work makes your life as a developer easier ☺.&lt;/p&gt;

&lt;p&gt;Thanks for reading this article, you're very welcome to suggest any fixes and improvements in the comments section below.&lt;/p&gt;

&lt;p&gt;Happy Solid coding. 👋&lt;/p&gt;

&lt;p&gt;Thanks to my friend "&lt;a href="https://dev.to/visionspider"&gt;David Guillaumant-Mergler&lt;/a&gt;" for suggesting improvements, and to the amazing folks on the &lt;a href="https://discord.com/invite/solidjs" rel="noopener noreferrer"&gt;Solid's Discord server&lt;/a&gt; for their considerable help and inexhaustible patience.&lt;/p&gt;




&lt;p&gt;Hey, you're also invited to join our small and fledging 🐥🐣 Discord community called &lt;strong&gt;TIL&lt;/strong&gt; (stands for "&lt;strong&gt;Today-I-Learned&lt;/strong&gt;") where we share things that we learn with each other, in the realm of web programming. Join using this &lt;a href="https://discord.gg/XxzKSgdSAD" rel="noopener noreferrer"&gt;link&lt;/a&gt;. &lt;/p&gt;

</description>
      <category>solidjs</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Multiple GitHub Accounts On One Machine - A Quick Guide 🤹‍♀️🚀</title>
      <dc:creator>aderchox</dc:creator>
      <pubDate>Sat, 25 Feb 2023 10:29:51 +0000</pubDate>
      <link>https://forem.com/aderchox/how-to-use-multiple-github-accounts-on-one-machine-a-quick-guide-ka3</link>
      <guid>https://forem.com/aderchox/how-to-use-multiple-github-accounts-on-one-machine-a-quick-guide-ka3</guid>
      <description>&lt;p&gt;Let's say we need one GitHub account for our personal use, and another one for our work/professional use. How do we do this?&lt;br&gt;
&lt;sup&gt;&lt;br&gt;
&lt;em&gt;&lt;strong&gt;NOTICE: Git is [thankfully] very strict on telling the truth about the history, and what's explained here is also not an exception. This method is NOT a way of "impersonating" one account with another! Rather "multi-account" here means "being able to use multiple accounts on the same machine SEPARATELY". If you don't know what all these mean, no worries, just ignore this notice.&lt;/strong&gt;&lt;/em&gt; &lt;br&gt;
&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;Here are the steps:&lt;/p&gt;

&lt;p&gt;👉1. Create and edit &lt;code&gt;~/.ssh/config&lt;/code&gt; on you machine (On Windows, that would mean a path like &lt;code&gt;C:\Users\&amp;lt;your-username&amp;gt;\.ssh\config&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;👉2. Add this to it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa
Host github.com-personal
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_personal
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;sup&gt;&lt;strong&gt;Explanation:&lt;/strong&gt; Basically what this config is saying is very simple, if it ever sees the string &lt;code&gt;git@github.com&lt;/code&gt; in a repository address, it will use the &lt;code&gt;~/.ssh/id_rsa&lt;/code&gt; private key, and if it sees the string &lt;code&gt;git@github.com-work&lt;/code&gt; in a repository address, it will use the &lt;code&gt;~/.ssh/id_rsa_work&lt;/code&gt; private key. You'll see an example in the end.&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;👉3. As you see, we need &lt;strong&gt;two pairs&lt;/strong&gt; of SSH keys (the ones which their private keys have been addressed by &lt;code&gt;IdentityFile&lt;/code&gt;s in the config snippet above). Now create these two pairs inside the &lt;code&gt;.ssh&lt;/code&gt; folder of your machine by &lt;code&gt;cd&lt;/code&gt;ing to it and running the below command twice:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd ~/.ssh
ssh-keygen -t rsa -b 4096 -C "your-work-email-address"
# After running the below command for creating the personal-use
# key pair, make sure to enter the correct name i.e. "id_rsa_personal",
# similar to the one that we used in the config.
ssh-keygen -t rsa -b 4096 -C "your-personal-email-address"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;sup&gt;NOTICE: When prompted for passphrases, you may only hit Enter on your keyboard to skip it.&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;👉4. Now add the public key of each of these pairs to its corresponding GitHub profile settings. I.e., one goes into your personal GitHub account's profile settings, and the other one goes into your work/professional one's profile settings.&lt;/p&gt;

&lt;p&gt;👉5. Now set the account that you use more as the global one for your machine's git account, and the one that you use less as the local one (on a per-project basis). E.g., I use the work account more, so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git config --global user.name &amp;lt;work-username&amp;gt;
git config --global user.email &amp;lt;work-email&amp;gt;
cd &amp;lt;a-specific-personal-project&amp;gt;
git config --local user.name &amp;lt;personal-username&amp;gt;
git config --local user.email &amp;lt;personal-email&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you like you can check and make sure all configs have been set properly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git config --global --list
git config --local --list
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;👉6. Now use &lt;code&gt;git@github.com&lt;/code&gt; for work/professional use, and &lt;code&gt;git@github.com-personal&lt;/code&gt; for personal use. E.g.: &lt;code&gt;git remote add origin git@github.com:username/repo.git&lt;/code&gt; when in work repositories, and &lt;code&gt;git remote add origin git@github.com-personal:username/repo.git&lt;/code&gt; when in personal repositories! 🤹‍♀️&lt;/p&gt;

&lt;p&gt;You're also invited to join our small fledging Discord community called TIL (stands for "Today-I-Learned") where we share things we learn and articles we write in the realm of web programming with each other. Join us using &lt;a href="https://discord.gg/ATp8gc7G" rel="noopener noreferrer"&gt;this invite link&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Thanks for reading this article, you're very welcome to suggest any fixes or improvements in the comments section below.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;Credit and Acknowledgment: Thanks &lt;a href="https://code.tutsplus.com/tutorials/quick-tip-how-to-work-with-github-and-multiple-accounts--net-22574" rel="noopener noreferrer"&gt;Jeffery Way&lt;/a&gt; for writing a guide on this, here I've only tried to explain it in a different way, by putting it in my own words, and summarizing it a bit.)&lt;/p&gt;

</description>
      <category>motivation</category>
      <category>worklifebalance</category>
      <category>career</category>
      <category>discuss</category>
    </item>
    <item>
      <title>Weird thing I missed about Async/Await: Implicit Returns</title>
      <dc:creator>aderchox</dc:creator>
      <pubDate>Sun, 17 Jul 2022 18:29:25 +0000</pubDate>
      <link>https://forem.com/aderchox/weird-thing-i-missed-about-asyncawait-implicit-returns-131m</link>
      <guid>https://forem.com/aderchox/weird-thing-i-missed-about-asyncawait-implicit-returns-131m</guid>
      <description>&lt;p&gt;Weird I didn't know this so far, because I've been using async/await syntax for a while now, but I've just realized this.&lt;/p&gt;

&lt;p&gt;Async functions return a Promise implicitly and automatically themselves. We don't need to do anything for this (e.g., we don't need to do &lt;code&gt;return new Promise((resolve)=&amp;gt;{/*...*/})&lt;/code&gt; inside our async functions (unless we explicitly need to)).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;br&gt;
This snippet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;is an equivalent of this snippet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For our comfort, JavaScript does the above conversion itself automatically.&lt;br&gt;
Note that &lt;em&gt;even if we return nothing&lt;/em&gt;, still the above conversion will take place, i.e.,:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(){}&lt;/span&gt;
&lt;span class="c1"&gt;// is an equivalent of:&lt;/span&gt;
&lt;span class="c1"&gt;// async function foo(){ return Promise.resolve(undefined); }&lt;/span&gt;

&lt;span class="c1"&gt;// Let's call foo to confirm our claim:&lt;/span&gt;
&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="c1"&gt;// Prints: Promise {&amp;lt;fulfilled&amp;gt;: undefined}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now why would understanding this be useful? Well personally I knew that whenever we use an &lt;code&gt;await&lt;/code&gt; keyword, it has to be kind of *coupled* with some Promise, so for instance we could do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;delayFoo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ms&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
  &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;resolve&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
      &lt;span class="nx"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nx"&gt;ms&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;delayFooThreeSeconds&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;delayFoo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// (*) This returns a Promise explicitly.&lt;/span&gt;

&lt;span class="c1"&gt;// now inside some async function:&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;demo&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;delayFooThreeSeconds&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// (*) This awaits the Promise returned explicitly by the call to delayFooThreeSeconds.&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;demo&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;delayFoo is an example of a function that is returning a Promise explicitly, but what if we need an async call within our function at a point that is not where it's returning? For example, consider a nested async/await where a function is both doing an async operation and is supposed to be awaited itself too inside another async function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;-- (*) This function is not returning any Promises explicitly.&lt;/span&gt;
  &lt;span class="nx"&gt;doSomething&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;delayFooThreeSeconds&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="nx"&gt;doSomethingElse&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;baz&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;--- (*) Would this be possible?&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the &lt;code&gt;bar&lt;/code&gt; function is not returning a Promise explicitly, so could it be awaited inside &lt;code&gt;baz&lt;/code&gt;? As you might have guessed the correct answer by now: "Yes", because the above function is returning a Promise implicitly. (Even if we have multiple async calls wrapped inside an async function, the hierarchy of the promises will be the same as the invocations of the async functions.)&lt;/p&gt;

&lt;p&gt;Here are some working examples for you to play with and see things better in action: &lt;a href="https://playcode.io/928113/"&gt;https://playcode.io/928113/&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;You're also welcome to join our very small and fledging Discord community where we share the things that we learn in the realm of programming with each other, it's called TIL (stands for "Today-I-Learned"). Join here: &lt;code&gt;https : // discord.gg / ATp8gc7G&lt;/code&gt;. Thanks for reading this article.&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
  </channel>
</rss>
