Skip to main content

My JavaScript Learning Journey - Week 9: Promises, API Calls, JavaScript Operators & Modules

Updated 2 min readMarkdown
Part of seriesJavaScript Learning Journey
My JavaScript Learning Journey - Week 9: Promises, API Calls, JavaScript Operators & Modules

Every week of my JavaScript learning journey introduces me to something new. Some concepts take time to understand, while others become clearer when I practise them with small examples.

During Week 9, I explored Callback Hell, Promises, API calls, Optional Chaining, Nullish Coalescing and JavaScript Modules.

I also started working on a new mini project called CountryExplorer, which gave me an opportunity to move beyond learning individual concepts and start building something practical.

In this article, I will share what I learned from Day 46 to Day 50, along with some simple examples that helped me understand these concepts.

Let's get started!

Day 46: Understanding Callback Hell and Promises

On Day 46, I learned about Callback Hell in JavaScript and why it can make code difficult to read and maintain.

Before understanding Callback Hell, I wanted to understand why callbacks are used in JavaScript.

What is a Callback?

A callback is a function passed as an argument to another function. It can be executed later, depending on how the receiving function is written.

Here is a simple example:

function greet(name, callback) {
  console.log(`Hello, ${name}!`);

  callback();
}

function sayWelcome() {
  console.log("Welcome to my website!");
}

greet("Dharmapada", sayWelcome);

Output:

Hello, Dharmapada!
Welcome to my website!

Here, sayWelcome is passed as an argument to the greet() function and executed inside it.

Callbacks are useful in JavaScript, especially when working with asynchronous operations.

What is Callback Hell?

Callback Hell happens when multiple callbacks are nested inside one another, creating deeply indented and difficult-to-manage code.

Let is understand this with a simple example:

function firstTask(callback) {
  console.log("First task completed");

  callback();
}

function secondTask(callback) {
  console.log("Second task completed");

  callback();
}

function thirdTask() {
  console.log("Third task completed");
}

firstTask(() => {
  secondTask(() => {
    thirdTask();
  });
});

About the author

  • Hi, I am Dharmapada, an MCA student and aspiring full-stack developer. I am currently learning modern web development with a focus on the MERN stack. I enjoy learning by building projects and experimenting with different technologies. My current journey includes HTML, CSS, JavaScript, React.js, Tailwind CSS, Node.js, Express.js, MongoDB and AI. I created this Hashnode profile to document what I learn, share my experiences and write about the challenges and solutions I discover while building projects. What I am Learning: 🌐 Frontend development with HTML, CSS & React ⚛️ React.js & Tailwind CSS 🛠️ Node.js & Express.js 🗄️ MongoDB & databases 🤖 AI and modern development tools 🚀 Building practical projects I will share tutorials, notes, project experiences and things I discover along the way. Learn -> Build -> Improve -> Repeat. 🚀

No comments yet. Be the first to comment.

More from this blog