Ipass 9
Ipass 9
Assignment 9
Aim:- Write a react program to implement the concept of Asynchronous programming using
promises
Lo Mapping:- LO5
Theory:-
1. Definition:
A promise is an object that represents the eventual completion or failure of an asynchronous
operation. It allows you to work with asynchronous code in a more manageable way by
providing a structured approach to handling results and errors.
2. States of a Promise:
Promises have three possible states:
- Pending: The initial state, indicating that the asynchronous operation is still ongoing.
- Fulfilled: Indicates that the operation completed successfully, providing a resulting value. -
Rejected: Indicates that the operation failed, providing a reason for the failure (typically an
error).
3. Key Methods:
- then(): This method is used to specify what to do when a promise is fulfilled. It takes a
callback function that receives the resolved value.
- catch(): This method is used to specify what to do when a promise is rejected. It takes a
callback function that receives the error.
- finally(): This method allows you to execute code after the promise is settled, regardless of
its outcome (fulfilled or rejected).
4. Promise Chaining:
Promises can be chained together, meaning that the output of one promise can be the input for
the next. This facilitates a sequence of asynchronous operations, allowing for cleaner code
organization.
5. Integration with React:
In React, promises are commonly used for handling asynchronous data operations, such as
fetching data from APIs. They are typically utilized within lifecycle methods (like
`useEffect`) or event handlers. The asynchronous nature of promises allows React
components to remain responsive while waiting for data to be fetched or processed.
6. Error Handling:
Name : Pratik Vishe
Roll No. : 100
Batch : T23
Promises provide a built-in way to handle errors through the `.catch()` method, which can be
particularly useful for managing errors in asynchronous operations without the need for
extensive try-catch blocks.
7. Async/Await:
The `async/await` syntax, which is syntactic sugar built on top of promises, provides a more
readable and straightforward way to work with asynchronous code in React, allowing you to
write asynchronous code that looks synchronous.
In summary, promises are an essential part of handling asynchronous operations in React,
providing a structured approach to managing the complexities of asynchronous programming.
Program:-
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
<title>React Async Programming with Promises</title>
<script
src="https://unpkg.com/react/umd/react.development.js"></script>
<script
src="https://unpkg.com/react-dom/umd/reactdom.development.js"></script
>
<script src="https://unpkg.com/babel-
standalone@6/babel.min.js"></script>
<style>
body { font-family: Arial, sans-
serif; margin: 20px;
}
h1 { color:
#333;
}
.loading {
Name : Pratik Vishe
Roll No. : 100
Batch : T23
}
.error { color:
red;
}
.post {
border: 1px solid #ccc;
padding: 10px;
margin-bottom: 10px;
border-radius: 5px;
} </style>
</head>
<body>
<div id="root"></div>
not ok');
}
Name : Pratik Vishe
Roll No. : 100
Batch : T23
return response.json(); })
.then((data) => resolve(data))
.then((data) => {
setData(data);
setLoading(false);
})
.catch((error) => {
setError(error.message);
setLoading(false);
});
}, []);
return (
<div>
<h1>Asynchronous Data Fetching with Promises</h1>
{loading && <p className="loading">Loading...</p>}
{error && <p className="error">Error: {error}</p>}
{data.length > 0 && (
<div>
{data.map((post) => (
<div className="post" key={post.id}>
<h2>{post.title}</h2>
<p>{post.body}</p>
</div>
))}
</div>
)}
Name : Pratik Vishe
Roll No. : 100
Batch : T23
</div>
);
};
Output:-