This repository contains various JavaScript and Node.js programs that demonstrate callback functions, promises, and asynchronous programming. Below is a detailed description of each program and instructions on how to run them.
- Callback Hell
- Coin Flip with Callback
- Asynchronous Addition
- Promise Flow Explanation
- Fetch Data from Public API
- Process Array with Delays
This program demonstrates a nested series of callbacks, often referred to as "callback hell." It simulates a student studying various topics in a sequential manner. The callbacks are nested to ensure each task completes before the next one starts.
This program simulates a coin flip using a callback function. The result of the coin flip ("heads" or "tails") is passed to the callback function.
This program converts a synchronous addition function to an asynchronous one using a callback. It simulates a delay before returning the sum of two numbers.
his program demonstrates the flow of a promise chain with .then and .catch blocks. It shows how promises can be used to handle asynchronous operations and errors.
-
The job function returns a promise that immediately rejects.
-
The promise is assigned to a variable.
-
The first .then block is skipped because the promise was rejected.
-
The second and third .then blocks are also skipped.
-
The .catch block executes, logging an error message.
-
The final .then block executes because the .catch handled the previous rejection.
This Node.js program fetches data from a public API using the Promise concept. It demonstrates how to make HTTP requests and handle responses asynchronously.
This Node.js program processes an array of numbers, doubling each number with a simulated delay of 1 second using Promises. It demonstrates how to chain promises to create delays between operations.
- Ensure you have Node.js installed.
- Save the code to a file named
callbackHell.js
. - Run the code using Node.js:
node callbackHell.js