Difference Between Observable and Promise in Angular 8
Join the DZone community and get the full member experience.Join For Free
Observable and Promise both provide us with abstractions that help us deal with the asynchronous nature of applications.
Promises work with asynchronous operations. They either return a single value (i.e the promise resolves) or an error message (i.e the promise rejects).
Another important thing to remember regarding promises is that a request initiated from a promise is not canceled.
Disadvantages of Promise:
- User could not cancel a request to the API.
- User could not retry a failed call.
- As our application gets bigger, promises become hard to manage.
An Observable is an array or a sequence of events over time. It has at least two participants, the creator (the data source) and the subscriber (subscription where data is being consumed). Compared to a promise, an observable can be canceled.
RxJS is all about unifying the ideas of promise callbacks and data flow and making them easier to work with. Observables provide operators, like
reduce...similar to an array.
There are also powerful operators like
- Basic knowledge of Angular.
- Visual Studio Code must be installed.
- Angular CLI must be installed.
- Node JS must be installed.
Here, with the backend-related code, we will use SQL Server.
The very first step is to create a database.
Let’s create a database on your local SQL Server. I hope you have installed SQL Server 2017 in your machine (you can use SQL Server 2008, 2012, or 2016, as well).
Create a Country table using the following code:
Now, let's add Store Procedures.
Create a Stored Procedure with the following code:
Create an ASP.NET Core application with the following steps.
In Visual Studio 2019, click on File -> New -> Project.
Choose the Create option and select ASP.NET web application.
Select Web API and click OK.
Now, right-click on the controller and add a new item.
Choose Ado.NET Entity Data Model, and then click on Add.
The next step is EF Designer, just click on Next.
A new pop-up will show. Click on Next. If yours isn't established, then click on a new connection
Copy your database connection server name and paste it in the server name textbox. You will see all the databases. Select your database and click on OK.
Here, in the new screen, select your tables and store the procedure. Then, click on finish:
Our next step is to right-click on the Controllers folder and add a new controller. Name it "CountryController" and add the following namespace in the CountryController.
Complete CountryController code
Let's create a new Angular project using the following NPM command,
Now, let's create a new component for "Observable " by using the following command:
Now, let's create a new component for "Promise" by using the following command,
Here, I am creating two different components one for observable example and another for promise. First, we will see the example for Promise.
Now, open the promise-example.component.html file and add the following code:
Now, open the promise-example.component.ts file and add the following code in this file:
The next step is to create a service.
Now, open the country.service.ts file and add the following code in this file:
Open file app.module.ts and paste the following code:
Now, open the app.component.html file and add the following code:
Now, it's time for the output:
As we can see, whenever I am typing the key, it's hitting the API every time (i.e it's going to the server every time and getting the data).
Example of Observable:
Now, open the observable-example.component.html file and add the following code in the file:
Now, open the observable-example.component.ts file and add the following code:
Replace the app.component.html file with the following code:
Now, its time for the output for Observable:
As we can see, as soon as I press the key, it's hitting the API every 400 milliseconds, so rather than hitting it on every keypress, it's hitting every 400-milliseconds using the
debounceTime operator in RxJS
So, with the help of RxJS operators, we can achieve this so that it can improve our application performance.
In this article, we have seen the difference between Observable and Promise in Angular 8 application.
Please give your valuable feedback/comments/questions about this article. Please let me know how to improve it.
Opinions expressed by DZone contributors are their own.