Javascript
/
⚡️ ⏐ Asynchronous
/
1. What are promises?

What is Async and Await?

Before we start on Async and Await, let's first understand what is a Promise.

What is a Promise?

A Promise is an object that represents the eventual completion (or failure) of an asynchronous operation, and its resulting value. I know this sounds a bit confusing, but let's break it down.

  • A promise is just like a promise you make to someone. You promise to do something, and you will either fulfill that promise or break it.
  • A promise is an object, which means it has properties and methods. We will look at these in a bit.
  • Before we talk about promises also we need to know why do we need promises?

Why do we need Promises?: We need promises because we need to do something outside of what javascript is doing. Let's say we are cooking some meal and we need salt and we don't have any.🙌🙌 Will we stop cooking and go to the shop to buy salt? No, we will ask someone to buy it for us. Either our kid or someone. We will ask them to buy salt and we will continue cooking.... this is promises in action. so we don't have to wait for the salt to come back from the shop. we continue cooking and when the salt comes back we will use it.

But the person we sent might come with the salt or not... so we need to handle that situation as well. How do we do this.. when the person brings the salt we use it and continue cooking if they don't we might cook without salt. Also if the person delays to bring the salt we might have finished and we don't need it... This is promises in action. 😁😁

Similary we need to make promises in javascript. We need to do something outside of what javascript is doing. That leads us to the next question. What will we be talking to? We will use a simple version which is we will be talking to an API. What is an API?🥲🥲

What is An API?

What is an API? An application programming interface is a URL that you can make requests to get information back from. It's like website but only for machines. It's a method that one computer can request information from another. Think of it as a gateway to get data. Similar to what happens in a hotel.🙌. In a hotel we have waiters and we have the people who process our orders and us the customers. Think of the waiter as an API because we give them our order and they go to the kitchen and bring us the food. similary we have same concept here. We make a request to an API asking for some data and the API may either respond with the data or not or may not even respond

What is Asynchronous Programming?

Asynchronous programming is a means of parallel programming in which a unit of work runs separately from the main application thread and notifies the calling thread of its completion, failure or progress.

What is Synchronous Programming?

Synchronous programming is a means of programming in which a unit of work runs one after the other. It's like a queue. You have to wait for the first one to finish before you can start the next one.

NOW let's look at an example: Now that we speak the language of APIs, let's dive into making some server requests. Let's walk through the hypothetical process in the next lesson

Last updated on October 14, 2023