PROMISE IN JAVASCRIPT
Promise In JavaScript – JavaScript Promise

In this blog you are going to learn promises in JavaScript. JavaScript promise is one of the important concept in JavaScript. Promises are used to perform asynchronous operations.

What Is Promise In JavaScript ?

A JavaScript Promise object can be:

  • Pending
  • Fulfilled
  • Rejected

What is a Promise in JavaScript?

In JavaScript, a Promise is an object representing the eventual completion or failure of an asynchronous operation.

What Is Synchronous In JavaScript ?

What Is Asynchronous In JavaScript ?

Important Points Regarding Promises

  • In JavaScript, a promise is a good way to handle asynchronous operations. It is used to find out if the asynchronous operation is successfully completed or not.
  • In JavaScript, a promise starts in a pending state. That means the process is not complete. If the operation is successful, the process ends in a fulfilled state. And, if an error occurs, the process ends in a rejected state.
  • For example, in JavaScript when you request data from the server by using a promise, it will be in a pending state. When the data arrives successfully, it will be in a fulfilled state. If an error occurs, then it will be in a rejected state
  • In JavaScript Promise, the then() method is used with the callback when the promise is successfully fulfilled or resolved.
  • In JavaScript Promise, the catch() method is used with the callback when the promise is rejected or if an error occurs

Source Code Of Promise In JavaScript

Promise.html

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
	
	<script>
	
		let p = new Promise(function(resolve, reject){
			
			document.write("Pending......");
			setTimeout(()=>{
				let a = 1 + 2;
			if(a == 3)
			 {
			   resolve("Success");
			 }
			else
				{
					reject("Failed");
				}
			},3000);
			
		});
		
		let onFulFil = (msg) => {
			document.write(msg + " - Promise FulFilled..");
		}
		let onReject = (msg) => {
			document.write(msg + " - Promise Rejected..");
		}
		p.then(onFulFil).catch(onReject);
		
//		p.then((msg) => {
//			document.write(msg + " - Promise FulFilled..");
//		}).catch((msg) => {
//			document.write(msg + " - Promise Rejected..");
//		})
		
//		console.log(p);
	
	</script>
	
</body>
</html>

Download Notes Of This Blog From the link given below

https://www.mediafire.com/file/tv1jrs3269nhp50/PROMISES.pptx/file

Download Source Code of This Blog From the link given below

https://www.mediafire.com/file/r8mdilig65lf0dr/Promise.html/file

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *