<?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>DEV Community: Jhaemis-hack</title>
    <description>The latest articles on DEV Community by Jhaemis-hack (@jhaemishack).</description>
    <link>https://dev.to/jhaemishack</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%2F1172408%2Fbbe4bacf-ea21-4490-9244-06d0ccdf1516.png</url>
      <title>DEV Community: Jhaemis-hack</title>
      <link>https://dev.to/jhaemishack</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jhaemishack"/>
    <language>en</language>
    <item>
      <title>How I Built and Consumed an External API Using FastAPI: A Practical Walkthrough</title>
      <dc:creator>Jhaemis-hack</dc:creator>
      <pubDate>Sat, 18 Oct 2025 02:25:13 +0000</pubDate>
      <link>https://dev.to/jhaemishack/how-i-built-and-consumed-an-external-api-using-fastapi-a-practical-walkthrough-1kcj</link>
      <guid>https://dev.to/jhaemishack/how-i-built-and-consumed-an-external-api-using-fastapi-a-practical-walkthrough-1kcj</guid>
      <description>&lt;h3&gt;
  
  
  The Inspiration
&lt;/h3&gt;

&lt;p&gt;I’ve been experimenting with &lt;strong&gt;FastAPI&lt;/strong&gt;, one of the most modern and performant Python frameworks for building web APIs.&lt;br&gt;&lt;br&gt;
This week, I decided to take it a bit further — not just build an API, but also &lt;strong&gt;consume another external API&lt;/strong&gt; inside my app, add &lt;strong&gt;rate limiting&lt;/strong&gt;, &lt;strong&gt;error handling&lt;/strong&gt;, and a touch of &lt;strong&gt;developer love&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;So in this post, I’ll walk you through exactly how I built a simple but structured FastAPI app that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Exposes routes like &lt;code&gt;/&lt;/code&gt;, &lt;code&gt;/health&lt;/code&gt;, and &lt;code&gt;/me&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Consumes an external cat-facts API 🐈
&lt;/li&gt;
&lt;li&gt;Implements rate limiting using &lt;strong&gt;SlowAPI&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Handles errors gracefully
&lt;/li&gt;
&lt;li&gt;Is ready for deployment with &lt;strong&gt;Procfile&lt;/strong&gt;, &lt;strong&gt;requirements.txt&lt;/strong&gt;, and &lt;strong&gt;pytest&lt;/strong&gt; tests
&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  Project Setup
&lt;/h2&gt;

&lt;p&gt;Let’s start from scratch.&lt;/p&gt;
&lt;h3&gt;
  
  
  1. Create and activate a virtual environment
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python &lt;span class="nt"&gt;-m&lt;/span&gt; venv .venv
&lt;span class="nb"&gt;source&lt;/span&gt; .venv/bin/activate  &lt;span class="c"&gt;# or on Windows: .venv\Scripts\activate&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  2. Install dependencies
&lt;/h3&gt;

&lt;p&gt;Your &lt;code&gt;requirements.txt&lt;/code&gt; should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fastapi
slowapi
pydantic_settings
httpx[h2]
uvicorn[standard]
pytest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then install:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-r&lt;/span&gt; requirements.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Building the App
&lt;/h2&gt;

&lt;p&gt;Let’s start with our &lt;code&gt;main.py&lt;/code&gt; — the heart of the app.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# main.py
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;fastapi&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;FastAPI&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Depends&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timezone&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;fastapi.responses&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;JSONResponse&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;fastapi.middleware.cors&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;CORSMiddleware&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;core.error_handlers&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;register_error_handlers&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;services.http_client&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;safe_http_request&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;core.exceptions&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;NotFoundException&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;slowapi&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Limiter&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;slowapi.errors&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;RateLimitExceeded&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;slowapi.util&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;get_remote_address&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;functools&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;lru_cache&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;typing_extensions&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Annotated&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;core&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;config&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;contextlib&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;asynccontextmanager&lt;/span&gt;


&lt;span class="nd"&gt;@asynccontextmanager&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;lifespan&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;FastAPI&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;App starting up...&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;startup_event&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;yield&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;App shutting down...&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;FastAPI&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lifespan&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;lifespan&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;My Profile App&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;register_error_handlers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="nd"&gt;@lru_cache&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_settings&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Settings&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;


&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;rate_limit_exceeded_handler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;exc&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;RateLimitExceeded&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;JSONResponse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;status_code&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;429&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;success&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;error&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Too many requests, please slow down.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="n"&gt;limiter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Limiter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key_func&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;get_remote_address&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;startup_event&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;limiter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;limiter&lt;/span&gt;
    &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_exception_handler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;RateLimitExceeded&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rate_limit_exceeded_handler&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_middleware&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;CORSMiddleware&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;allow_origins&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;*&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="n"&gt;allow_credentials&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;allow_methods&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;*&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="n"&gt;allow_headers&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;*&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="n"&gt;Profile_details&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;email&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;gbemilekekenny@gmail.com&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;name&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;James Kehinde&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;stack&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;NodeJs&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="nd"&gt;@app.get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;home&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;JSONResponse&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;success&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;message&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Welcome to my profile API&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;


&lt;span class="nd"&gt;@app.get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/health&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;health_check&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;JSONResponse&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;success&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;message&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Ok&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;


&lt;span class="nd"&gt;@limiter.limit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;8/minute&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nd"&gt;@app.get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/me&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_profile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;settings&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Annotated&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Settings&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Depends&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;get_settings&lt;/span&gt;&lt;span class="p"&gt;)]):&lt;/span&gt;
    &lt;span class="n"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;settings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cat_api_url&lt;/span&gt;
    &lt;span class="n"&gt;fact&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;safe_http_request&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;GET&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;max_length&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;fact&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;NotFoundException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Timeout, try again later.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;status&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;success&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;user&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Profile_details&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;timestamp&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;timezone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;utc&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;isoformat&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;+00:00&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Z&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;fact&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;fact&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;fact&lt;/span&gt;&lt;span class="sh"&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="nc"&gt;JSONResponse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="nd"&gt;@app.get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/favicon.ico&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;include_in_schema&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;favicon&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status_code&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;204&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Key Takeaways from the Implementation
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. &lt;strong&gt;Rate Limiting&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Using &lt;code&gt;slowapi&lt;/code&gt;, I restricted requests to &lt;code&gt;/me&lt;/code&gt; to &lt;strong&gt;8 per minute&lt;/strong&gt; per client.&lt;br&gt;&lt;br&gt;
This prevents abuse and adds robustness.&lt;/p&gt;
&lt;h3&gt;
  
  
  2. &lt;strong&gt;External API Consumption&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;I used an async HTTP client wrapper (&lt;code&gt;httpx&lt;/code&gt;) to fetch cat facts from an external API.&lt;br&gt;&lt;br&gt;
The idea was to simulate consuming a third-party service within my API.&lt;/p&gt;
&lt;h3&gt;
  
  
  3. &lt;strong&gt;Error Handling&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Custom error handlers are registered in &lt;code&gt;core/error_handlers.py&lt;/code&gt;, ensuring consistent JSON responses instead of raw stack traces.&lt;/p&gt;
&lt;h3&gt;
  
  
  4. &lt;strong&gt;Lifespan Events&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Instead of deprecated &lt;code&gt;@app.on_event("startup")&lt;/code&gt;, I used &lt;strong&gt;FastAPI’s lifespan context manager&lt;/strong&gt; — the modern, recommended approach.&lt;/p&gt;


&lt;h2&gt;
  
  
  Testing the App with Pytest
&lt;/h2&gt;

&lt;p&gt;To make sure everything works smoothly, I added &lt;code&gt;test_main.py&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;fastapi.testclient&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;TestClient&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt;

&lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;TestClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_home_route&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status_code&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&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;assert&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;success&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run tests with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pytest &lt;span class="nt"&gt;-v&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Running the App Locally
&lt;/h2&gt;

&lt;p&gt;To start your server locally:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;uvicorn main:app &lt;span class="nt"&gt;--reload&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your app will be live at:&lt;br&gt;
👉 &lt;a href="http://127.0.0.1:8000" rel="noopener noreferrer"&gt;http://127.0.0.1:8000&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can visit:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;/&lt;/code&gt; → Welcome route
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;/health&lt;/code&gt; → Health check
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;/me&lt;/code&gt; → Profile + external cat fact
&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  🚢 Deployment Ready
&lt;/h2&gt;

&lt;p&gt;If deploying on &lt;strong&gt;Render&lt;/strong&gt;, &lt;strong&gt;Railway&lt;/strong&gt;, or &lt;strong&gt;Heroku&lt;/strong&gt;, your &lt;code&gt;Procfile&lt;/code&gt; should look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;web: uvicorn main:app --host=0.0.0.0 --port=${PORT:-8000}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Wrapping Up
&lt;/h2&gt;

&lt;p&gt;In this small but practical project, I:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Built a clean and modern FastAPI service
&lt;/li&gt;
&lt;li&gt;Integrated an external API with async HTTP calls
&lt;/li&gt;
&lt;li&gt;Implemented rate limiting
&lt;/li&gt;
&lt;li&gt;Structured the app for production and deployment
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This project gave me a clearer view of &lt;strong&gt;API consumption patterns&lt;/strong&gt; inside FastAPI, and how easy it is to scale small ideas into something production-ready.&lt;/p&gt;




&lt;h3&gt;
  
  
  Author
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;James Kehinde&lt;/strong&gt; — Full-Stack Developer | Node.js + FastAPI Enthusiast | Building meaningful software from Lagos 🇳🇬&lt;/p&gt;

&lt;p&gt;💬 Let’s connect on LinkedIn or Twitter if you’re building something cool with FastAPI!&lt;/p&gt;

</description>
      <category>fastapi</category>
      <category>python</category>
      <category>api</category>
      <category>backend</category>
    </item>
    <item>
      <title>deciphering The "req" Object in Node.Js</title>
      <dc:creator>Jhaemis-hack</dc:creator>
      <pubDate>Mon, 16 Dec 2024 22:12:06 +0000</pubDate>
      <link>https://dev.to/jhaemishack/deciphering-the-req-object-in-nodejs-4pg5</link>
      <guid>https://dev.to/jhaemishack/deciphering-the-req-object-in-nodejs-4pg5</guid>
      <description>&lt;p&gt;Have you also been wandering what Exactly does "req" and "res" do in your express application, What are the methods I can utilize with it. Have you also been taunted with the fact that we do not only have req.body, but also req.params... apart from this two, what other methods does it have? Well, you are not alone (...Obviously). In this post, I will be helping you to decipher what exactly "req" and "res" does in your express app, and methods associated with REQUEST object.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The 'req' (request) object in Express JS is used to represent the incoming HTTP request that consists of data like parameters, query strings, request body and so on.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Some Other Usefulness Include:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Accessing the Request Data: By using the req object, we can access the various components of the incoming HTTP request which consists of data, headers, parameters, etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Middleware Interaction: middleware functions can change and modify the request object by allowing various tasks of logging and authentication.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Routing: By implementing the dynamic routes in the application, the req object can be used as it captures the URL parameters and also allows it to respond dynamically based on the client's input.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;While, on the Other hand, "res" (Response) object is used to send the HTTP response to the client which allows the modification of headers and consists of status codes, and resources.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Few of it's Usefulness include: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Send Responses: The res object is used to send the HTP responses to the client which includes the resource, data, status codes, and headers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Error Handling: we can send the error responses which is important for handling the errors and providing feedback to the client.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Content Modification: We can also set the custom response headers, status codes, and content through which the response control can be managed by us.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;Now, let's Jump to the Main Focus of this post, REQUEST METHODS!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you have been following along, you should now have a little glimpse of what "req" is and does in your code. Howbeit, the question still stands, are there any other methods apart from &lt;code&gt;req.body&lt;/code&gt; and &lt;code&gt;req.params&lt;/code&gt;? I will begin to dig that up for you now.&lt;/p&gt;

&lt;p&gt;Methods Associated with REQ. Object:&lt;br&gt;
Majorly, there are four methods directly associated with &lt;code&gt;req&lt;/code&gt; object. They are: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;req.body&lt;/li&gt;
&lt;li&gt;req.params&lt;/li&gt;
&lt;li&gt;req.query&lt;/li&gt;
&lt;li&gt;req.headers&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;However, there are also some few properties often confused to be part of the unique methods inbuilt with &lt;code&gt;req&lt;/code&gt; object. such include:&lt;br&gt;
'&lt;code&gt;req.method&lt;/code&gt;', '&lt;code&gt;req.url&lt;/code&gt;', '&lt;code&gt;req.cookies&lt;/code&gt;', '&lt;code&gt;req.ip&lt;/code&gt;', ...&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;code&gt;req.body&lt;/code&gt;:
&lt;/h2&gt;

&lt;p&gt;This method allows us to access all the data sent from the front end to the backend by the client in the body of a POST, PUT, or PATCH request. Hence requires a middleware, like express.json() or bodyParser.json() to parse it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.post('/login', (req, res) =&amp;gt; {
    console.log(req.body); // Logs user credentials sent in the body
});

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  req.query:
&lt;/h2&gt;

&lt;p&gt;Stores, as a subsidiary of &lt;code&gt;req.body&lt;/code&gt;, query parameters right after ? joined to the URL. Often used for filtering or searching datas.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// URL: /search?name=John
app.get('/search', (req, res) =&amp;gt; {
    console.log(req.query.name); // Outputs 'John'
});

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  req.params:
&lt;/h2&gt;

&lt;p&gt;Holds route parameters defined in the URL paths. It is often indicated by a &lt;code&gt;:&lt;/code&gt; symbol. It is typically used for dynamic routes. It will automatically get whatsoever is placed in that position in the URL path.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// URL: /user/123
app.get('/user/:id', (req, res) =&amp;gt; {
    console.log(req.params.id); // Outputs '123'
});

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  req.headers:
&lt;/h2&gt;

&lt;p&gt;this method Contains headers sent by the client, often used for authentication or content negotiation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.get('/data', (req, res) =&amp;gt; {
    console.log(req.headers['authorization']); // Outputs the auth token
});

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I hope you can boast yourself of understanding the functionality of "res" and "req" in your express app now.&lt;br&gt;
Thank you for stopping by.&lt;/p&gt;

</description>
      <category>reqobject</category>
      <category>resobject</category>
      <category>reqbody</category>
      <category>reqparams</category>
    </item>
    <item>
      <title>Async Vs Sync, which is most preferrable?</title>
      <dc:creator>Jhaemis-hack</dc:creator>
      <pubDate>Mon, 02 Dec 2024 10:34:36 +0000</pubDate>
      <link>https://dev.to/jhaemishack/async-vs-sync-which-is-most-preferrable-16e6</link>
      <guid>https://dev.to/jhaemishack/async-vs-sync-which-is-most-preferrable-16e6</guid>
      <description>&lt;p&gt;According to the description of JavaScript(Js), we have been informed that Js is a single threaded language that enforces blocking programming model which will prevent codes from running haphazardly or randomly(i.e, runs in order from the first line of code to the last line of code). &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Hence, in a block of codes JavaScript run the last line of code last.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;However, an additional functionality that set JavaScript aside from every other language is the ability of the codes to run in no particular order. A good illustration will be to consider a restaurant ordering system. Let's say we have six(6) customers in the restaurant, and they all ordered for different delicacies.&lt;br&gt;
the first ordered for pounded yam&lt;br&gt;
the second order for white yam&lt;br&gt;
the third ordered for white rice&lt;br&gt;
the fourth, jollof rice&lt;br&gt;
the fifth, beans&lt;br&gt;
and the sixth, jollof spaghetti.&lt;/p&gt;

&lt;p&gt;conventionally, the chefs should attend to the first customer, then the second and on and on to the sixth customer. That's the way normal Js works. Until it finishs running the first line of code the second won't run and if the fifth throws an error, it will never read the sixth line of code. However, if you have ever been to a standard restaurant, it just doesn't work that way. Rather than waiting for pounded yam to get served first, other cook in the kitchen will asynchronously start preparing for the other dishes that won't take more time. So it is possible for the sixth customer who requested for jollof spaghetti to get served first. &lt;/p&gt;

&lt;p&gt;That is just how the asynchronous property of Js works. If the fourth line of code is set to async, Js will not wait for it before running the fifth and sixth line. Hence, this allows a non-blocking functionality in Js.&lt;/p&gt;

&lt;p&gt;There are basically three forms of async model:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;async-await&lt;/li&gt;
&lt;li&gt;.then .Catch&lt;/li&gt;
&lt;li&gt;.finally()&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  async-await
&lt;/h2&gt;

&lt;p&gt;A modern and clean way to work with promises. It makes asynchronous code look and behave more like synchronous code. await pauses the execution of the function until the promise resolves or rejects.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async function fetchData() {
    try {
        const response = await fetch('https://jsonplaceholder.typicode.
com/posts/1'); // Wait for the fetch to complete
        const data = await response.json(); // Wait for the response to be parsed as JSON
        console.log('Data:', data); // Log the fetched data
    } catch (error) {
        console.error('Error:', error); // Handle any errors
    }
}

fetchData();

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Declared an &lt;code&gt;async&lt;/code&gt; function.&lt;br&gt;
Allows the use of &lt;code&gt;await&lt;/code&gt; inside the function.&lt;br&gt;
Pauses the execution of the function until the promise resolves.&lt;br&gt;
&lt;code&gt;await fetch(...)&lt;/code&gt; waits for the HTTP request to complete.&lt;br&gt;
&lt;code&gt;await response.json()&lt;/code&gt; waits for the response body to be parsed as JSON.&lt;br&gt;
&lt;code&gt;try...catch&lt;/code&gt; handles any errors that occur during the asynchronous operation.&lt;br&gt;
For example, if the network request fails, the error will be caught and logged.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;An async function declaration creates an AsyncFunction object. Each time &lt;br&gt;
when an async function is called, it returns a new Promise which will be resolved with the value returned by the async function, or rejected with an exception uncaught within the async function.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Async can also be used as a function.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Async functions can contain zero or more await expressions. Await expressions make promise-returning functions behave as though they're synchronous by suspending execution until the returned promise is fulfilled or rejected. The resolved value of the promise is treated as the return value of the await expression. Use of async and await enables the use of ordinary &lt;code&gt;try/catch&lt;/code&gt; blocks around asynchronous code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async function getProcessedData(url) {
  let v;
  try {
    v = await downloadData(url);
  } catch (e) {
    v = await downloadFallbackData(url);
  }
  return processDataInWorker(v);
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  .then .catch
&lt;/h2&gt;

&lt;p&gt;This shows how &lt;code&gt;.then()&lt;/code&gt; is used to chain promise-based operations and handle asynchronous data fetching&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fetch('https://jsonplaceholder.typicode.com/posts/1')
    .then(response =&amp;gt; {
        if (!response.ok) {
            throw new Error('Network response was not ok');
        }
        return response.json(); // Parse the response as JSON
    })
    .then(data =&amp;gt; {
        console.log('Data:', data); // Log the fetched data
    })
    .catch(error =&amp;gt; {
        console.error('Error:', error); // Handle any errors
    });

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Initiates an HTTP request to the provided URL.&lt;br&gt;
Returns a promise that resolves to the HTTP response.&lt;br&gt;
If successful, the response is parsed as JSON using &lt;code&gt;response.json()&lt;/code&gt;, &lt;br&gt;
Second &lt;code&gt;.then()&lt;/code&gt; Block: Handles the parsed JSON data from the first &lt;code&gt;.then()&lt;/code&gt;.&lt;br&gt;
Logs the data to the console.&lt;br&gt;
catch Block: Handles errors that occur in any of the &lt;code&gt;.then()&lt;/code&gt; blocks or during the fetch call.&lt;br&gt;
Logs the error message to the console&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  .finally()
&lt;/h2&gt;

&lt;p&gt;It ensures that a specific block of code runs no matter what happens in the promise.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fetch('https://jsonplaceholder.typicode.com/posts/1')
    .then(response =&amp;gt; {
        if (!response.ok) {
            throw new Error('Network response was not ok');
        }
        return response.json(); // Parse the response as JSON
    })
    .then(data =&amp;gt; {
        console.log('Data:', data); // Log the fetched data
    })
    .catch(error =&amp;gt; {
        console.error('Error:', error); // Handle any errors
    })
    .finally(() =&amp;gt; {
        console.log('Request completed.'); // Executes regardless of success or failure
    });

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;The &lt;code&gt;.finally()&lt;/code&gt; block is executed after the promise is settled, regardless of whether it was fulfilled or rejected.&lt;br&gt;
It’s useful for cleanup tasks, such as hiding a loading spinner or resetting variables, that should run no matter what.&lt;br&gt;
If the promise is fulfilled: The &lt;code&gt;.then()&lt;/code&gt; blocks execute, followed by &lt;code&gt;.finally()&lt;/code&gt;.&lt;br&gt;
If the promise is rejected: The &lt;code&gt;.catch()&lt;/code&gt; block executes, followed by &lt;code&gt;.finally()&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now, you can decide which is best for your project.&lt;/p&gt;

</description>
      <category>asynchronous</category>
      <category>synchronous</category>
      <category>async</category>
      <category>sync</category>
    </item>
    <item>
      <title>Still Don't Understand Loops? Look No Further.</title>
      <dc:creator>Jhaemis-hack</dc:creator>
      <pubDate>Tue, 26 Nov 2024 10:11:55 +0000</pubDate>
      <link>https://dev.to/jhaemishack/still-dont-understand-loops-look-no-further-4eh</link>
      <guid>https://dev.to/jhaemishack/still-dont-understand-loops-look-no-further-4eh</guid>
      <description>&lt;p&gt;In this post, I will be addressing all you concerns about what loops  are and how they work. Whichever programming language you are currently learning, whether it is Java, Python, JavaScript, C#, c++, e.t.c, they all follow the same nomenclature of action. I intend to help you trash out the concept of Loops on your Journey to becoming a programmer. In this post, I will make use of JavaScript as a reference point to whichever Language you are learning.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Loop?
&lt;/h2&gt;

&lt;p&gt;According to Zuckerberg's Chat Bot(Meta AI): A loop is a control structure in programming that allows a sequence of instructions to be executed repeatedly, either for a fixed number of iterations or until a specific condition is satisfied.&lt;/p&gt;

&lt;p&gt;I like this definition as it makes explicit indication - "Until a specific condition is satisfied". To redefine it, Loops are blocks of code that will not stop iterating over and over unless an already declared condition becomes false (i.e, condition is no longer true). &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;When constructing a loop, a condition of action is always declared and the loop will not be terminated unless the declared condition is no longer true. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There are basically, 2 major types of Loops in all programming languages:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;For Loop&lt;/li&gt;
&lt;li&gt;While Loop&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;However, In JavaScript there are 5 forms of Loops:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;For Loop&lt;/li&gt;
&lt;li&gt;For Of Loop&lt;/li&gt;
&lt;li&gt;For In Loop&lt;/li&gt;
&lt;li&gt;While Loop&lt;/li&gt;
&lt;li&gt;Do While Loop&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I will focus on those 2 major ones in this post while also making references to the other forms we have in Javascript.&lt;/p&gt;

&lt;h2&gt;
  
  
  FOR LOOP
&lt;/h2&gt;

&lt;p&gt;As I have already established, Loops are used for repeatedly running some blocks of code. However, they all have their best used cases. FOR loops are mostly used to iterate over a sequence, such as an array, object, set, or string.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you want to individually work with all the members of an array or object, For Loop might just be your best choice. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here is a simple structure of a For Loop&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Define an array

let numbers = [1, 2, 3, 4, 5];

// Use a For loop to iterate over the array

for(let i = 0; i &amp;lt; numbers.length; i++) {

  // Print each number

  console.log(numbers[i]);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;We define an array &lt;code&gt;numbers&lt;/code&gt; containing five integers.&lt;br&gt;
The For loop iterates over the &lt;code&gt;numbers&lt;/code&gt; array.&lt;br&gt;
The variable &lt;code&gt;i&lt;/code&gt; is the loop counter, initialized to &lt;code&gt;0&lt;/code&gt;.&lt;br&gt;
The loop condition checks if &lt;code&gt;i&lt;/code&gt; is less than the length of the numbers array which is 5.&lt;br&gt;
Inside the loop, we print the current value of &lt;code&gt;numbers[i]&lt;/code&gt; using &lt;code&gt;console.log()&lt;/code&gt;&lt;br&gt;
Immediately &lt;code&gt;i&lt;/code&gt; becomes &amp;gt; than &lt;code&gt;numbers.length&lt;/code&gt;, it becomes false and terminates the Loop&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The same is also the case if it is an arrays of strings. However, when working with arrays of strings, it is most advisable to make use of &lt;code&gt;FOR OF&lt;/code&gt; loop.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const myArray = ["lucy", "lumzy", "rex", "apex", "wixy"];

for (const item of myArray) {
    console.log(item);
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;This will print each element of &lt;code&gt;myArray&lt;/code&gt; to the console.&lt;br&gt;
the &lt;code&gt;item&lt;/code&gt;is standing as an index representator for all the element in &lt;code&gt;myAarray&lt;/code&gt;, beginning from the first to the last&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And if it is an Object, it's best to make use of a &lt;code&gt;FOR IN&lt;/code&gt; Loop.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const myObject = { a: 1, b: 2, c: 3 };

for (const key in myObject) {
    console.log(`Key: ${key}, Value: ${myObject[key]}`);
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;The &lt;code&gt;for...in&lt;/code&gt; loop iterates over the keys of the &lt;code&gt;myObject&lt;/code&gt;.&lt;br&gt;
The variable &lt;code&gt;key&lt;/code&gt; holds the current key or property (e.g., a, b, c).&lt;br&gt;
The value of the current key is accessed using &lt;code&gt;myObject[key]&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In JavaScript, a &lt;code&gt;for...in&lt;/code&gt; loop is used to iterate over the keys (or properties) of an object. Reference can also be made to the values using the keys.&lt;/p&gt;

&lt;p&gt;Take your time to read through the For Loop below to better understand how it works.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Define an object

let students = [
  { name: 'John Doe', age: 20, grade: 85 },
  { name: 'Jane Doe', age: 22, grade: 90 },
  { name: 'Bob Smith', age: 21, grade: 78 },
  { name: 'Alice Johnson', age: 20, grade: 92 },
];

// Use a For loop to iterate over the array

for (let i = 0; i &amp;lt; students.length; i++) {
  // Check if the student's grade is above 85 and age is above 21

  if (students[i].grade &amp;gt; 85 &amp;amp;&amp;amp; students[i].age &amp;gt; 21) {
    // Print the student's name and grade

    console.log(`${students[i].name} has a grade of ${students[i].grade}`);

  } else if (students[i].grade &amp;gt; 85 &amp;amp;&amp;amp; students[i].age &amp;lt;= 21) {
    // Print a message if the student's grade is above 85 but age is 21 or below

    console.log(`${students[i].name} is a high-achieving student, but not yet 22 years old.`);

  } else {
    // Print a message if the student's grade is 85 or below

    console.log(`${students[i].name} needs to improve their grade.`);
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  WHILE LOOP
&lt;/h2&gt;

&lt;p&gt;While loop is less preferred by most programmers due to its unique conditional cases. Unlike For loop, While Loops can both be  used with a Boolean and numeric conditional cases.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Initialize a counter variable

let i = 0;

// Set a condition for the loop

while (i &amp;lt; 5) {

  console.log(`Counter: ${i}`)// Print the current value of the counter

  i++;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using a Boolean test case as conditions allows programmers a lot of unique usage of the Loop. &lt;/p&gt;

&lt;p&gt;Here is a simple code block for a while loop:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let count = 0;

while (count &amp;lt; 5) {
    console.log(`Count is: ${count}`);

    count++; // Increment the count
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;The loop continues as long as the value of &lt;code&gt;count&lt;/code&gt; is less than &lt;code&gt;5&lt;/code&gt;.&lt;br&gt;
&lt;code&gt;count++;&lt;/code&gt; Increases the value of &lt;code&gt;count&lt;/code&gt; by 1 on each iteration.&lt;br&gt;
When count reaches 5, the condition &lt;code&gt;count &amp;lt; 5&lt;/code&gt; becomes false, and the loop stops.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;However, While loop also works well with a Boolean Conditional case. Here is an example of such:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let isRunning = true;

while (isRunning) {
    console.log("The loop is running...");

    isRunning = false; // Change the condition to stop the loop
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;After the first iteration, &lt;code&gt;isRunning&lt;/code&gt; is set to &lt;code&gt;false&lt;/code&gt;, so the loop condition is no longer satisfied, and the loop ends.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Similar to &lt;code&gt;While&lt;/code&gt; loop is &lt;code&gt;do-While&lt;/code&gt; loop. The only difference between the two is that &lt;code&gt;While&lt;/code&gt; Loop condition is checked before the loop executes (i.e, the loop body may not run if the condition is false initially - "Check first, then execute"), whilest, &lt;code&gt;Do-While&lt;/code&gt; Loop executes at least once before the condition is checked, regardless of whether the condition is true or false.&lt;/p&gt;

&lt;p&gt;Here is a simple do-While Loop code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let count = 0;

do {
    console.log(`Count is: ${count}`);
    count++;
} while (count &amp;lt; 5);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Check up this While loop to better understand how it works in application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let isLoggedIn = false;

// Simulate user login attempt

while (!isLoggedIn) {

  let username = prompt("Enter your username:");
  let password = prompt("Enter your password:");

  // Verify login credentials 

  if (username === "admin" &amp;amp;&amp;amp; password === "password") {
    isLoggedIn = true; //sets the condition true

    console.log("Login successful!");

  } else {

    console.log("Invalid username or password. Please 
try again.");

  }
}

// User is now logged in

console.log("Welcome to the dashboard!");

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;a boolean variable &lt;code&gt;isLoggedIn&lt;/code&gt; is initialized to false.&lt;br&gt;
The While loop checks the condition &lt;code&gt;!isLoggedIn&lt;/code&gt;, which is equivalent to &lt;code&gt;(isLoggedIn === false)&lt;/code&gt;.&lt;br&gt;
If the condition is true, the loop body executes&lt;br&gt;
If the credentials are valid, sets &lt;code&gt;isLoggedIn&lt;/code&gt; to true.&lt;br&gt;
Steps 2-3 repeat until the condition &lt;code&gt;!isLoggedIn&lt;/code&gt; is false.&lt;br&gt;
Once the condition is false, the loop exits, and the user is logged in.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;One common mistake programmers make with &lt;code&gt;WHILE&lt;/code&gt; Loops is that they define the condition within the loop, which eventually crashes the terminal.&lt;/p&gt;

&lt;p&gt;If you have followed me through up to this point, congratulations you have just divulged the most important points you need to know before effectively using a Loop.&lt;/p&gt;

</description>
      <category>loops</category>
      <category>forloop</category>
      <category>whileloop</category>
      <category>javascriptloops</category>
    </item>
  </channel>
</rss>
