Async And Await In JavaScript
Async And Await In JavaScript

In This Blog, you are going to learn one of the most important topics in JavaScript i-e Async And Await, Synchronous And Asynchronous Programming In JavaScript. For Performing asynchronous Operations then we can use promises and async and await in JavaScript.

Synchronous Programming

Asynchronous Programming

JavaScript Async And Await

  • In JavaScript, there’s a special syntax to work with promises in a more comfortable fashion, called “async/await”. It’s surprisingly easy to understand and easy to use.
  • In JavaScript, async and await make promises easier to write
  • In JavaScript, async makes a function return a Promise.
  • In JavaScript, the keyword async before a function makes the function return a promise.

Both Functions Are Same

Demonstration of Promise In JavaScript

Async Function

Async With Await In JavaScript

Important Points – Await & Async

  • In JavaScript, await makes a function wait for a Promise
  • In JavaScript, Promises introduced in es6 2015
  • In JavaScript, Async & await introduced in es 2017
  • In JavaScript, we don’t need to write resolve and reject inside async function because async function will take care of it.
  • In JavaScript, the await keyword can only be used inside an async function, you cannot use await keyword inside a normal function.

Source Code

Async_Await.html File Code

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>ASYNC AND AWAIT</title>
</head>

<body>
	
	<script>
	
//		FETCH - CALLING API

//		async function example()
//		{
//			console.log("hello 1");
//			const data = await fetch("http://jsonplaceholder.typicode.com/posts");
//			console.log("hello 2");
//			const posts = await data.json();
//			console.log("hello 3");
//			return posts;
//		}
		
		async function example()
		{
		
			const data = await fetch("http://jsonplaceholder.typicode.com/posts");
			
			const posts = await data.json();
			
			return posts;
		}
		
		example().then((result) => {
			console.log(result);
		}).catch((msg) => {
			console.log(msg);
		});
		
//		let a = example();
//		console.log(a);
//		console.log("hello 4");
//		console.log("hello 5");
		
//		async function example()
//		{
//			console.log("hello 1");
//			await console.log("hello 2");
//			console.log("hello 3");
//		}
//		
//		example();
//		console.log("hello 4");
//		console.log("hello 5");
		
//		async function example()
//		{
//			return "Hello World";
//		}
//		
//		console.log(example());
		
//	console.log("hello 1");
//	setTimeout(() => {
//		console.log("hello 2");
//	},3000);
//	console.log("hello 3");
	
	</script>
	
</body>
</html>

Download Notes Of This Blog From the link given below

https://www.mediafire.com/file/2oz2kceouajrbx7/ASYNC+AWAIT.pptx/file

Download Source Code of This Blog From the link given below

https://www.mediafire.com/file/khdojli44rcoipy/ASYNC_AWAIT.html/file

No responses yet

Leave a Reply

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