JavaScript
JavaScript callback functions
▪ A callback function can be defined as any function passed into
another function as an argument, and then invoked within that
function to complete a task. This can be done with both named
and anonymous functions.
synchronous and asynchronous functions
▪ Synchronous Functions : A synchronous function executes
sequentially, line-by-line. Each statement waits for the previous
one to complete before executing. This means that synchronous
functions are blocking: a slow operation (like a network request or
a large calculation) can block the rest of the code from executing
until it completes.
▪ Asynchronous Functions : An asynchronous function allows
JavaScript to start an operation and move on to the next task
without waiting for it to complete. This is non-blocking behavior,
meaning the code can continue executing without waiting for the
asynchronous task to [Link] functions are often
used for tasks like network requests, file reading, or time-based
delays.
JavaScript callback Hell
▪ When many callbacks are nested within each other, it can lead to
"callback hell," which makes the code difficult to read and
maintain.
JavaScript Promise
▪ In JavaScript, Promises are a powerful way to handle asynchronous
operations, such as fetching data from a server, reading files, or
waiting for user input. Promises allow you to work with asynchronous
code in a more structured, readable way, and they help avoid deeply
nested callbacks, known as "callback hell.“
▪ What is a Promise?
▪ A Promise is an object that represents the eventual completion (or
failure) of an asynchronous operation and its resulting value. A
Promise can be in one of three states:
▪ Pending: The initial state; the operation is still ongoing.
▪ Fulfilled: The operation completed successfully, and the Promise has a
resolved value.
▪ Rejected: The operation failed, and the Promise has a reason for the
failure.
JavaScript Promise
▪ Creating a Promise
▪ To create a Promise, use the Promise constructor and pass in a
function (known as the executor) with two parameters: resolve and
reject. The resolve function is called if the operation is successful, and
the reject function is called if it fails.
JavaScript Promise
▪ Handling Promises with .then() and .catch() :
▪ Program can handle the outcome of a Promise using the .then() and .catch()
methods:
▪ .then() is used to handle a fulfilled (resolved) Promise.
▪ .catch() is used to handle a rejected Promise.
Fetch API
▪ The Fetch API is a modern interface for making HTTP requests in JavaScript,
allowing you to easily retrieve data from a server or send data to one. It's built
on top of Promises, so it provides a simpler, cleaner way to handle
asynchronous operations compared to older methods like XMLHttpRequest.
▪ The fetch() function returns a Promise that resolves to the response of the
request.
▪ url: The endpoint you’re fetching from.
▪ options (optional): An object that can include method, headers, body, and
other configuration options.