<?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: Nava</title>
    <description>The latest articles on Forem by Nava (@namraj).</description>
    <link>https://forem.com/namraj</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%2F186370%2Fa6df6cfc-156d-4110-b706-3e583a00b20b.jpg</url>
      <title>Forem: Nava</title>
      <link>https://forem.com/namraj</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/namraj"/>
    <language>en</language>
    <item>
      <title>The Python Guide: Virtual Environments, pip, and Everyday Workflows</title>
      <dc:creator>Nava</dc:creator>
      <pubDate>Sat, 21 Feb 2026 11:45:30 +0000</pubDate>
      <link>https://forem.com/namraj/the-python-guide-virtual-environments-pip-and-everyday-workflows-dcf</link>
      <guid>https://forem.com/namraj/the-python-guide-virtual-environments-pip-and-everyday-workflows-dcf</guid>
      <description>&lt;p&gt;Python is one of the most widely used languages in the world, but a clean development workflow matters just as much as the code itself. This guide covers the essentials — virtual environments, dependency management, and handy tricks that save time day to day.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Setting Up a Virtual Environment with &lt;code&gt;venv&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Virtual environments keep each project's dependencies isolated from one another and from your system Python. Every project should have its own — it prevents version conflicts and makes your setup reproducible on any machine.&lt;/p&gt;

&lt;p&gt;Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir projectA &amp;amp;&amp;amp; cd projectA

python3.8 -m venv env

source env/bin/activate    # Linux / macOS

env\Scripts\activate.bat  # Windows CMD

env\Scripts\Activate.ps1  # Windows PowerShell
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  To exit the environment:
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;deactivate&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
Running &lt;code&gt;python3.8 -m venv env&lt;/code&gt; creates an &lt;code&gt;env/&lt;/code&gt; folder containing an isolated Python binary, pip, and a lib/ directory for installed packages. Always add &lt;code&gt;env/&lt;/code&gt;to your &lt;code&gt;.gitignore&lt;/code&gt; — it should never be committed to version control.&lt;/p&gt;
&lt;h2&gt;
  
  
  2. Managing Dependencies with pip
&lt;/h2&gt;

&lt;p&gt;pip is Python's package installer. The key workflow is to install what you need, then freeze those dependencies into a requirements.txt file so anyone else can reproduce your exact environment with a single command.&lt;/p&gt;

&lt;p&gt;Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install Flask             # install a package

pip list                   # see installed packages

pip freeze &amp;gt; requirements.txt  # save exact versions

pip install -r requirements.txt # restore from file
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To uninstall and cleanly reinstall all dependencies — useful when something breaks — run pip uninstall package-name followed by pip install -r requirements.txt. This is often the fastest way to resolve mysterious dependency conflicts.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. &lt;code&gt;virtualenvwrapper&lt;/code&gt;for Convenience
&lt;/h2&gt;

&lt;p&gt;If you manage many projects, virtualenvwrapper is a layer on top of venv that gives you short commands to create, switch, list, and delete environments from anywhere on your system — no need to navigate into the project folder first.&lt;/p&gt;

&lt;p&gt;Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip3 install `virtualenvwrapper`

mkvirtualenv my-project   # create

workon my-project       # activate

lsvirtualenv           # list all environments

rmvirtualenv my-project   # delete

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

&lt;/div&gt;



&lt;p&gt;You can confirm which environment is currently active at any time by running echo $VIRTUAL_ENV — it prints the full path to the active environment, or nothing if none is active.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Spin Up a Quick HTTP Server
Python ships with a built-in HTTP server that's useful for serving static files locally — great for previewing HTML or sharing a folder over a local network without installing anything extra.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Example&lt;/p&gt;

&lt;p&gt;&lt;code&gt;python -m http.server 8000&lt;br&gt;
&lt;/code&gt; &lt;br&gt;
Serves the current directory at &lt;code&gt;http://localhost:8000&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  5. Clearing the Python Interpreter
&lt;/h2&gt;

&lt;p&gt;When working interactively in the Python REPL, the screen can fill up quickly. There's no built-in clear command, but you can use &lt;code&gt;os.system()&lt;/code&gt; to call the terminal's clear command. A cross-platform one-liner works on both Windows and Unix.&lt;/p&gt;

&lt;p&gt;Example — cross-platform clear&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import os

clear = lambda: os.system('cls' if os.name == 'nt' else 'clear')

clear()  # call it any time

os.name == 'nt'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;evaluates to True on Windows and False on Linux or macOS, so the same function works everywhere without any changes.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Python in WSL / Ubuntu on Windows
&lt;/h2&gt;

&lt;p&gt;When running Python projects inside WSL (Windows Subsystem for Linux), your Windows files are accessible under &lt;code&gt;/mnt/c/&lt;/code&gt;. Navigate there from the Ubuntu terminal to work on projects stored in your Windows filesystem.&lt;/p&gt;

&lt;p&gt;Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd /mnt/c/Users/yourname/dev/my-project

python3 -m venv env

source env/bin/activate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you run into issues with Node-based tools (like &lt;code&gt;Vite&lt;/code&gt;) inside WSL, a common fix is to delete &lt;code&gt;node_modules&lt;/code&gt; and reinstall from within the Linux environment, or set CHOKIDAR_USEPOLLING=true to work around file-watcher mismatches between Windows and Linux.&lt;/p&gt;

&lt;p&gt;A solid Python workflow comes down to three habits: always use a virtual environment, keep your requirements.txt up to date, and know your tools. The commands above cover the majority of what you'll need for day-to-day Python development.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>beginners</category>
      <category>python</category>
    </item>
    <item>
      <title>Responsive grid layout</title>
      <dc:creator>Nava</dc:creator>
      <pubDate>Sun, 26 Mar 2023 12:59:08 +0000</pubDate>
      <link>https://forem.com/namraj/responsive-grid-layout-43l7</link>
      <guid>https://forem.com/namraj/responsive-grid-layout-43l7</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Responsive design involves Writing code that targets different screen sizes with @media rule. A web page may have a menu on top right with a logo on left for a simplest form which looks cool on desktop or laptop. Responsive design cares about how same menu looks on a smaller screens like iPad or mobile on resize.Bootstrap provides convenient classes like .sm-col-6, or .md-col-8 to specify width of element on small or medium devices.&lt;/p&gt;

&lt;p&gt;'''css&lt;br&gt;
@media only screen and (max-width:601) {&lt;br&gt;
.logo, .nav li {&lt;br&gt;
display: block;&lt;br&gt;
Width: 100%&lt;br&gt;
}&lt;br&gt;
}&lt;br&gt;
'''&lt;/p&gt;

&lt;p&gt;In css grid, we have different grid-template-areas property that allows to define areas in the page that can be applied to individual div containers like header,#main-content or footer like,&lt;/p&gt;

&lt;h1&gt;
  
  
  main-content {
&lt;/h1&gt;

&lt;p&gt;grid: content &lt;/p&gt;

</description>
    </item>
    <item>
      <title>Simple Web development workflow</title>
      <dc:creator>Nava</dc:creator>
      <pubDate>Sun, 26 Mar 2023 12:58:14 +0000</pubDate>
      <link>https://forem.com/namraj/simple-web-development-workflow-31bc</link>
      <guid>https://forem.com/namraj/simple-web-development-workflow-31bc</guid>
      <description>&lt;h1&gt;
  
  
  The folder structure
&lt;/h1&gt;

&lt;p&gt;Any development work needs some organised plans.In my project I am  up files in a new folder starts with creating folder and files before coding for web development, in my case it looks like this. &lt;/p&gt;

&lt;h2&gt;
  
  
  Project folder
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;images&lt;/li&gt;
&lt;li&gt;app

&lt;ul&gt;
&lt;li&gt;style.scss&lt;/li&gt;
&lt;li&gt;script.js&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;dist

&lt;ul&gt;
&lt;li&gt;style.css&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;index.html&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Q6S8oFN9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://github.com/Namrajp/Product-Landing/blob/master/images/vscode-folderStructure.png%3Fraw%3Dtrue" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Q6S8oFN9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://github.com/Namrajp/Product-Landing/blob/master/images/vscode-folderStructure.png%3Fraw%3Dtrue" alt="Product-landing"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Customizing and Plugins in Visual code
&lt;/h2&gt;

&lt;p&gt;Changing default Settings in order to customize visual studio code is easy.To do that, I go to bottom left in settings, I choose &lt;code&gt;font-size&lt;/code&gt; to be large enough to see clearly so I chose it &lt;code&gt;18px&lt;/code&gt;, You may choose different depending on your needs. The &lt;code&gt;tabs-size 2&lt;/code&gt; instead of default of 4, and &lt;em&gt;word wrap to yes&lt;/em&gt;.&lt;br&gt;
Installing plugins from &lt;em&gt;Extensions Marketplace&lt;/em&gt;&lt;br&gt;
The basic plugins are &lt;em&gt;Live server&lt;/em&gt; and &lt;em&gt;Sass live serve&lt;/em&gt;. The &lt;em&gt;javascript snippets for Html css and Js&lt;/em&gt; plugins is also helpful.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DODGKIw3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://github.com/Namrajp/Product-Landing/blob/master/images/vscode-settings.png%3Fraw%3Dtrue" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DODGKIw3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://github.com/Namrajp/Product-Landing/blob/master/images/vscode-settings.png%3Fraw%3Dtrue" alt="vscode-settings"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Common shortcuts and buttons
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Shift-Alt-DOWN which copies whole line down&lt;/li&gt;
&lt;li&gt;CTRL-D which selects similar tags in between lines&lt;/li&gt;
&lt;li&gt;CTRL-B hide sidebar&lt;/li&gt;
&lt;li&gt;CTRL-p search files or Alt-Tab&lt;/li&gt;
&lt;li&gt;CTRL-R to browse between folders&lt;/li&gt;
&lt;li&gt;Shift-CTRL-p opens control pallete for shortcuts keys or settings project workspace or any plugins.&lt;/li&gt;
&lt;li&gt;Go Live button to open live server&lt;/li&gt;
&lt;li&gt;Watch Sass to enable watch sass to convert to css&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bBf1OLQK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://github.com/Namrajp/Product-Landing/blob/master/images/vscode-shortcutKeys.png%3Fraw%3Dtrue" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bBf1OLQK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://github.com/Namrajp/Product-Landing/blob/master/images/vscode-shortcutKeys.png%3Fraw%3Dtrue" alt="shortcutKeys"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Pulling together files for build
&lt;/h2&gt;

&lt;p&gt;I usually setup a app folder for sass files and dist folder for style-sheets file. The live sass compiler compiles the sass files and generate the &lt;code&gt;style.css&lt;/code&gt; in dist folder. The app folder contains style.sass as main sass file which pulls together other sass partial files like &lt;code&gt;_globals.sass&lt;/code&gt;, &lt;code&gt;_variables.sass&lt;/code&gt;, &lt;code&gt;_mixins.sass&lt;/code&gt;, &lt;em&gt;typography.sass and other sass files.The _debugging in google chrome&lt;/em&gt; is very helpful to see if the final style.css file is working.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;These settings help me a lot to get started, I hope It helps someone to learn using the tools and get started in the journey. All the best.&lt;/p&gt;

</description>
      <category>vscode</category>
      <category>css</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Vscode Shortcuts and Extensions commonly used</title>
      <dc:creator>Nava</dc:creator>
      <pubDate>Sun, 26 Mar 2023 12:57:12 +0000</pubDate>
      <link>https://forem.com/namraj/vscode-shortcuts-and-extensions-commonly-used-5e4h</link>
      <guid>https://forem.com/namraj/vscode-shortcuts-and-extensions-commonly-used-5e4h</guid>
      <description>&lt;h1&gt;
  
  
  Shortcuts
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;All Commands Pallette - cmd + shft + p&lt;/li&gt;
&lt;li&gt;Files - cmd + p&lt;/li&gt;
&lt;li&gt;Toggle terminal - ctrl + `(backtick)&lt;/li&gt;
&lt;li&gt;Toggle sidebar - cmd + b&lt;/li&gt;
&lt;li&gt;Comment selected - cmd + /&lt;/li&gt;
&lt;li&gt;Indent selected - cmd + } or {&lt;/li&gt;
&lt;li&gt;Select multiple lines hold alt and select&lt;/li&gt;
&lt;li&gt;Select similar tags - alt + d
&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Extensions
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Code Formatter - Prettier&lt;/li&gt;
&lt;li&gt;Icons - Material icon theme&lt;/li&gt;
&lt;li&gt;Live Server - Live SASS compiler - Browser Sync&lt;/li&gt;
&lt;li&gt;Theme dark - One Dark Pro Atoms - Mariana sublime text theme&lt;/li&gt;
&lt;li&gt;HTML CSS Support - CSS Intellisense&lt;/li&gt;
&lt;li&gt;Tailwind CSS intellisense&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>Accessing Mongo database in Node app</title>
      <dc:creator>Nava</dc:creator>
      <pubDate>Sun, 26 Mar 2023 12:56:36 +0000</pubDate>
      <link>https://forem.com/namraj/accessing-mongo-database-in-node-app-5d2b</link>
      <guid>https://forem.com/namraj/accessing-mongo-database-in-node-app-5d2b</guid>
      <description>&lt;p&gt;MongoDB is no-sql non-relational database. Means data is stored as documents similar to objects in javascript.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installation of MongoDB in Mac
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;$ brew install mongodb-community&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;$ brew services start mongoDb&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;local install in /usr/local/var/mongodb, /usr/local/etc/ for config. ip=127.0.0.1&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Commands to see database and collections
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;start shell $ mongo, db shows test database, use dbname,&lt;/li&gt;
&lt;li&gt;db, use, show databases, show collections.&lt;/li&gt;
&lt;li&gt;collections is same like tables.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Operations on local database
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;create data&lt;code&gt;db.createCollection('cars')&lt;/code&gt; or other way &lt;code&gt;db.dogs.insert({ name: "Roger" })&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;&lt;code&gt;db.dogs.find()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;filter and retrieve entry &lt;code&gt;$ db.dogs.find({name: 'Roger'}):&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;findOne()&lt;/code&gt;,&lt;/li&gt;
&lt;li&gt;update data&lt;code&gt;db.dogs.remove({})&lt;/code&gt; removes all entries.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;db.dogs.update({key: 'old obj'}, {key: 'new obj'})&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;remove data&lt;code&gt;db.dogs.remove({name: 'sakira'})&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Remote mongo database setup using Atlas
&lt;/h2&gt;

&lt;p&gt;Atlas is remote database as a service.managed, hosted.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create account, create db user, use ip, 0.0.0.0/0 connect from everywhere , get db connected, authId.&lt;/li&gt;
&lt;li&gt;Create Model, first schema, is shape of document.&lt;/li&gt;
&lt;li&gt;.env for url, to use invironment var we need .env file with variable='string', we install dotenv, require ('dotenv').config()&lt;/li&gt;
&lt;li&gt;npm install mongodb, mongoose, dotenv, express. require them in .json.package.also, nodemon,&lt;/li&gt;
&lt;li&gt;&lt;code&gt;mongoose.connect(url, {topology})&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;const personSchema = new mongoose.Schema({}&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Person = mongoose.model('Person',personSchema);&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  we created schema personSchema and we created model Person
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We can instantiate Model Person&lt;br&gt;
then call save() with callback err,data parameters inside handler function, we call callback done(null, data). we call done, it inside again. That's it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;we use Model.create() for many, use instance,Model.save() for single person. awe, difference.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Model.Method takes first parameter obj, second calback.So, two callbacks, one for handler fn, and other from Model.method()&lt;br&gt;
some methods save,create,find,findOne,findById,findOneAndUpdate,findByIdAndRemove, callbacks done,done(null, data),function(err, data) {} etc to list in MongoDB &amp;amp; Mongoose package.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Model.Method takes first parameter obj, second calback.So, two callbacks, one for handler fn, and other from Model.method()
// some methods save,create,find,findOne,findById,findOneAndUpdate,findByIdAndRemove, callbacks done,done(null, data),function(err, data) {} etc to list in MongoDB &amp;amp; Mongoose package.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Using MongoDB from Node.js
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;To interface to a MongoDB database from a Node.js app&lt;/li&gt;
&lt;li&gt;init -y, npm install mongodb&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;const mongo = require('mongodb').MongoClient&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;try&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;client&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;mongo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;connect&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="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;useNewUrlParser&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;span class="na"&gt;useUnifiedTopology&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;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&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="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&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;ul&gt;
&lt;li&gt;&lt;code&gt;Now you can select a database using the client.db() method:&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;select a database &lt;code&gt;const db = client.db('kennel')&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;can get a collection &lt;code&gt;const collection = db.collection('dogs')&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;one &lt;code&gt;const result = await collection.insertOne({name: 'Roger'})&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;many &lt;code&gt;const result = await collection.insertMany([{name: 'Togo'}, {name: 'Syd'}])&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;find &lt;code&gt;const items = await collection.find().toArray()&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We need a handle either as a function or const variable collection,&lt;br&gt;
or db.collectionName. then we add dot method.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rest API uses routes HTTP GET / and HTTP POST /
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;In Express we create server, web server&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;app.get('/', (req, res) )&lt;/code&gt; to send to client ,get data from database or from script.&lt;/li&gt;
&lt;li&gt;single data as &lt;code&gt;app.get('/:id', (req, res) )&lt;/code&gt; handled in script using &lt;code&gt;filter(cb)&lt;/code&gt;
from &lt;code&gt;req.params&lt;/code&gt;. and send back using &lt;code&gt;res.status(code).json()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;app.post('/', (req, res) )&lt;/code&gt; to insert data to db.&lt;/li&gt;
&lt;li&gt;we use mongodb or postgre sql database to store local or remote.&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>Callbacks and Promises in Asynchronous Programming</title>
      <dc:creator>Nava</dc:creator>
      <pubDate>Wed, 07 Jul 2021 02:27:16 +0000</pubDate>
      <link>https://forem.com/namraj/callbacks-and-promises-in-asynchronous-programming-njb</link>
      <guid>https://forem.com/namraj/callbacks-and-promises-in-asynchronous-programming-njb</guid>
      <description>&lt;p&gt;&lt;strong&gt;Asynchronous Programming means running programs without waiting for execution line by line&lt;/strong&gt;. We can move onto next line during execution. If one line accessing data from database or server is taking wait time and we donot want to halt entire program.&lt;/p&gt;

&lt;p&gt;Browsers provide API's like setTimeout, promise, Async-await, fetch API, Storage API for Asynchronous programs.These are commonly called by the name &lt;a href="https://en.wikipedia.org/wiki/Web_API"&gt;Web API&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In backend there are other api's like &lt;a href="https://en.wikipedia.org/wiki/Representational_state_transfer"&gt;REST&lt;/a&gt; api, graphQl api.&lt;/p&gt;

&lt;h2&gt;
  
  
  Callbacks and setTimeout function
&lt;/h2&gt;

&lt;p&gt;Callbacks are very useful in application programs.For a simple example,We can use callbacks using &lt;code&gt;setTimeout&lt;/code&gt; function. We can see most functions in javascript use callbacks like&lt;code&gt;forEach&lt;/code&gt;,&lt;code&gt;fetch&lt;/code&gt;,&lt;code&gt;promise&lt;/code&gt;,&lt;code&gt;addEventListener&lt;/code&gt;, &lt;code&gt;async&lt;/code&gt; &lt;code&gt;await&lt;/code&gt;,&lt;code&gt;map&lt;/code&gt;, &lt;code&gt;filter&lt;/code&gt;, &lt;code&gt;reduce&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  setTimeout
&lt;/h2&gt;

&lt;p&gt;The example here has three &lt;code&gt;setTimeout&lt;/code&gt; functions. Each function takes an argument. Each of the arguments are functions themselves. They are called callbacks.Each of them prints a string on console. First timer function executes after one second, second after five seconds and third after two seconds. So, that means javascript executes first then executes third &lt;code&gt;console.log&lt;/code&gt; because it is set to run after two seconds. And then third one after five seconds.&lt;/p&gt;

&lt;p&gt;Javascript has something called &lt;strong&gt;event loop&lt;/strong&gt;, to handle asynchronous code.When you call an asynchronous function in JavaScript, that is queued into the event loop, and will only be executed when its turn comes.This prevents blocking execution of other programs. So, to make every process isolated from each other, every tabs of browser, each node.js programs, API calls, Web Workers have their own  event loop.&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="nx"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&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="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;Hello&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="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&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="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;Dear come later.&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="mi"&gt;5000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&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="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;World&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="mi"&gt;2000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The execution of code does not care about the order &lt;code&gt;setTimeout&lt;/code&gt; functions are written in code. It follows the timer which controls the order the outputs are executed.  Event loop executes third &lt;code&gt;setTimeout&lt;/code&gt; function by loading it in call stack after it executes and removes first function from call stack.&lt;/p&gt;

&lt;p&gt;(&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JDFocP8_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z05k2f8ogoglsd5zx85r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JDFocP8_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z05k2f8ogoglsd5zx85r.png" alt="Callbacks"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Callback&lt;/strong&gt;&lt;br&gt;
Most programs that use callback has to perform operations in an order, for example, if we access data from database using API and then so some operation on data and then display the result.&lt;br&gt;
This code above prints results without any sequence because the timer is asynchronous. To display output in sequence, we can do use same code by nesting setTimeout functions into one.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: The main point is second setTimeout depends on result of first function and result of third function depends on result of second &lt;code&gt;setTimeout&lt;/code&gt; function.&lt;br&gt;
&lt;/p&gt;


&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&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="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;Hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nx"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&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="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;It comes sooner.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&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="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;World&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="mi"&gt;2000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;5000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--arIbvy6m--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9y0iewh8kzoomp8ggzfh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--arIbvy6m--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9y0iewh8kzoomp8ggzfh.png" alt="Callback in sequence"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the following example we use callback &lt;code&gt;cb&lt;/code&gt; as parameter of handleName function. We use callback to call string functions &lt;code&gt;makeUpperCasename&lt;/code&gt; and &lt;code&gt;reverseName&lt;/code&gt; as argument along with a string name. The handle name function takes the argument name and replace the parameter with it, and it takes a callback which is invoked inside the function. The result is uppercase fullname and reverse fullname.&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="nx"&gt;makeUpperCasename&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="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="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toUpperCase&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// makeUpperCasename("Johathon");&lt;/span&gt;

&lt;span class="nx"&gt;handleName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Jimmy&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;makeUpperCasename&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;handleName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Jimmy&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reverseName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;reverseName&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="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="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;split&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="nx"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;join&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="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;handleName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;cb&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;fullName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; Carter `&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;cb&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fullName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;cb&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fullName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;cb&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fullName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="c1"&gt;//  two gotchas, we donot invoke `cb` in function parameter&lt;/span&gt;
  &lt;span class="c1"&gt;//  and we can pass any functions as `cb` when we invoke it&lt;/span&gt;
  &lt;span class="c1"&gt;// and any number of time.&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Ha2xCJka--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x1yn5ry0ubujrz3b4s4f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Ha2xCJka--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x1yn5ry0ubujrz3b4s4f.png" alt="Callbacks Example"&gt;&lt;/a&gt;)&lt;br&gt;
Callbacks are very useful in most cases.The example we use here is simple one, in real world, in application programs code using callback are less manageable when we have so many callbacks. It is called callback hell. The simplest solution to this is promises.&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;btn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&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;btn&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;btn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&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="p"&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="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;Hello World!&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="c1"&gt;// console.log(btn);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Promise
&lt;/h2&gt;

&lt;p&gt;We define promise using new constructor which takes a callback as a parameter. We have an arrow function as callback. &lt;br&gt;
The callback has two parameters &lt;code&gt;resolve&lt;/code&gt; and &lt;code&gt;reject&lt;/code&gt; which are methods of Promise API.We call these methods inside the curly braces in our logic.If we have a error in our logic we call &lt;code&gt;reject&lt;/code&gt; and if the code is doing well we use &lt;code&gt;resolve&lt;/code&gt;.&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;promise&lt;/span&gt; &lt;span class="o"&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="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&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="c1"&gt;// resolve("I am a resolve of promise");&lt;/span&gt;
  &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I am a reject of promise&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;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="nx"&gt;promise&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We need to call promise after defining. We do that using handlers &lt;code&gt;.then&lt;/code&gt; and &lt;code&gt;.catch&lt;/code&gt;.Both &lt;code&gt;catch&lt;/code&gt; and &lt;code&gt;then&lt;/code&gt; takes callback functions as arguments. &lt;/p&gt;

&lt;p&gt;We need to handle errors, inside &lt;code&gt;catch&lt;/code&gt; method using a parameter that we can name whatever we like, here, I use error parameter, then we print error. We print result if there is no error inside the &lt;code&gt;then&lt;/code&gt; method.&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="nx"&gt;promise&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;error&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="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&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="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;data&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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;h2&gt;
  
  
  Async await
&lt;/h2&gt;

&lt;p&gt;Async await provides similar functionality like promises with a nicer syntax.&lt;br&gt;
Any function can become async function by using &lt;code&gt;async&lt;/code&gt; keyword prefix. We call promise using &lt;code&gt;await&lt;/code&gt; afterwards. We have assigned &lt;code&gt;data&lt;/code&gt; as constants to store result returned.&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;getProducts&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="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&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="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&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="c1"&gt;// get products data &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;fulfillling promises&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="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;doSomething&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&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;getProducts&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="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Fetch API
&lt;/h2&gt;

&lt;p&gt;Fetch provides networking in Browser. We have access to response and request objects of HTTP using fetch. &lt;code&gt;HTTP&lt;/code&gt; is the API that connects our browsers to the web server. &lt;br&gt;
Just a fetch on a &lt;code&gt;url&lt;/code&gt; returns a promise.We need to handle the promise returned.&lt;br&gt;
 We need use &lt;code&gt;async&lt;/code&gt; function and &lt;code&gt;await&lt;/code&gt; to make sense of data returned. Other option is to handle promise with then and catch methods.&lt;br&gt;
&lt;code&gt;const result = fetch(url)&lt;/code&gt;&lt;br&gt;
&lt;code&gt;console.log(result)&lt;/code&gt;&lt;br&gt;
Calling just fetch returns promise, so we use await before fetch and try catch block and enclose everything inside async function.we are using &lt;code&gt;IIFE&lt;/code&gt; in this example.&lt;code&gt;IIFE&lt;/code&gt; is immediately invoked function expression that invokes itself.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;fetch&lt;/code&gt; call makes a request to the server and returns a &lt;code&gt;response&lt;/code&gt; object in body of the message. Next step is to convert that &lt;code&gt;response&lt;/code&gt; into human readable &lt;code&gt;data&lt;/code&gt;.Finally we are using &lt;code&gt;.json&lt;/code&gt; method on the &lt;code&gt;response&lt;/code&gt; object returned by the fetch network call. Then we print the &lt;code&gt;data&lt;/code&gt; as our result in the console.&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="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async&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="k"&gt;try&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="nx"&gt;fetch&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&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="nx"&gt;json&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="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&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="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>callbacks</category>
      <category>promises</category>
      <category>asyncawait</category>
    </item>
    <item>
      <title>
Do CSS Grid replace Flexbox?</title>
      <dc:creator>Nava</dc:creator>
      <pubDate>Mon, 27 Jul 2020 06:01:51 +0000</pubDate>
      <link>https://forem.com/namraj/do-css-grid-replace-flexbox-4chf</link>
      <guid>https://forem.com/namraj/do-css-grid-replace-flexbox-4chf</guid>
      <description>&lt;p&gt;&lt;strong&gt;Grid&lt;/strong&gt; is better than &lt;strong&gt;flexbox&lt;/strong&gt; for a number of valid points. &lt;em&gt;There are things grid do better than flexbox and vice versa&lt;/em&gt;.On March 2017 with less browser support than flexbox grid was released unprefixed and ready-to-go, in Chrome, Opera, Firefox, and Safari.&lt;br&gt;
Grid is better for whole page layouts than flexbox because using flexbox for layout can cause content shift during loading due to js modifying the DOM which can be used for &lt;a href="https://jakearchibald.com/2014/dont-use-flexbox-for-page-layout/"&gt;tools not rules.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Grid can add columns or rows on fly with &lt;code&gt;grid-template-rows&lt;/code&gt; or &lt;code&gt;grid-template-columns&lt;/code&gt; and use &lt;code&gt;grid-gap&lt;/code&gt; property to make home made style page. The flexbox on other hand use its content to layout the page using &lt;code&gt;flex-wrap&lt;/code&gt; and &lt;code&gt;flex-basis&lt;/code&gt; which can essentially implement flex-grow or &lt;code&gt;flex-shrink&lt;/code&gt; the boxes.&lt;/p&gt;

&lt;p&gt;Furthermore, Grid uses the auto layout, minmax(), repeat(), and auto-fill which can provide responsive media query alternative.&lt;br&gt;
&lt;em&gt;Flexbox is essentially for laying out items in a single dimension – in a row OR a column. Grid is for layout of items in two dimensions – rows AND columns.&lt;/em&gt;&lt;br&gt;
&lt;iframe height="600" src="https://codepen.io/sandrina-p/embed/mZXWYN?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

</description>
      <category>grid</category>
      <category>flexbox</category>
    </item>
    <item>
      <title>Positioning a navbar and logo in Landing Page</title>
      <dc:creator>Nava</dc:creator>
      <pubDate>Tue, 30 Jun 2020 05:51:52 +0000</pubDate>
      <link>https://forem.com/namraj/positioning-a-navbar-and-logo-in-landing-page-151p</link>
      <guid>https://forem.com/namraj/positioning-a-navbar-and-logo-in-landing-page-151p</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Positioning is often overlooked property in CSS. This may be because the elements normally follow the flow of the document and positioning is not always needed.The elements of an HTML document can be either block elements like p,div or h1 that spans through entire line or inline elements like span or images which fills the surrounding space in a container.There are four basic positioning properties that are static, relative, absolute and fixed. &lt;/p&gt;

&lt;h2&gt;
  
  
  Static and Fixed
&lt;/h2&gt;

&lt;p&gt;The static is dafault positioning which is normal flow of elements.The fixed positioning places an element at a certain position which remains unchanged on scroll or resizing the browser window.Fixed positioning removes an element from the normal flow of the document, which is similar to absolute positioning.In example, an unordered list is positioned fixed at bottom of browser window with 20px pixels from right.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;

&lt;span class="nt"&gt;ul&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;fixed&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;bottom&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;right&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;20px&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;h2&gt;
  
  
  Relative positioning
&lt;/h2&gt;

&lt;p&gt;The other two positioning that needs more illustrations are relative and absolute positioning. The relative positioning is basically moves an element from the original position(normal flow of document) to a new position specifically dictated by values of left, right, bottom and top  properties. Relative positioning does not affect the surrounding elements or parent elements.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;

&lt;span class="nc"&gt;.nav-bar&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;relative&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;top&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;right&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0px&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;In above code, lets assume a menu with class .nav-bar is pushed from normal flow to top-right corner from its usual position.&lt;/p&gt;

&lt;h2&gt;
  
  
  Absolute positioning
&lt;/h2&gt;

&lt;p&gt;In contrast, the below code places the element  shifted with coordinates (20px, 25px) from the left corner of the browser window.Coordinates for absolute elements are always relative to the closest container that is a positioned element. &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;

&lt;span class="nc"&gt;.logo&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;absolute&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;top&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;25px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;left&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;20px&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;So, if we change .logo's parent element which may be a header element to be relatively positioned, it should appear in the position 25px from top and 20px  from the left of its parent element instead the browser window.&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%2Ffreeweblearning.com%2Fwp-content%2Fuploads%2F2019%2F10%2FCss-Background-Animation.jpg" 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%2Ffreeweblearning.com%2Fwp-content%2Fuploads%2F2019%2F10%2FCss-Background-Animation.jpg" alt="Free Web Learning Background animation"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>positioning</category>
      <category>cssbasics</category>
      <category>layout</category>
      <category>visualdesign</category>
    </item>
  </channel>
</rss>
