What is the Difference Between Promises and Observables in Angular ?
Last Updated :
24 Apr, 2025
Angular is a TypeScript-based Single-Page Application framework used to create dynamic and responsive web applications. It is one of the most popular and robust frameworks. One of its key features is to handle Asynchronous Operations effectively. In Asynchronous, code is not executed sequentially rather it follows multi-threading. When we make an HTTP request, the next code after that request need not wait for it to finish execution. The request can be running in the background simultaneously without blocking the remaining code. Some of the scenarios where we need Asynchronous Programming are when making HTTP requests to fetch data from the server, and real-time data streams where data need to be retrieved continuously like in the stock market, running concurrent applications, etc. This can be done in two ways i.e., Promise and Observable.
Promise
A Promise represents a single value in the future, that may not be available at present but is expected to be resolved or rejected in the future. It is more readable and maintainable in asynchronous. A Promise object has two possible states, i.e. the resolve and reject. It offers a structured way to handle resolved or rejected states. It has "then ()" to handle resolved states and "catch ()" to handle rejected ones. These help in making promises a suitable choice for single asynchronous operations. Suitable for activities such as reading data from server, files. There are 4 phases in it, namely, fulfilled, rejected, pending, and settled.
- Pending: In this, action is still pending and not yet fulfilled or rejected.
- Fulfilled: This state represents that the asynchronous operation is successfully completed.
- Rejected: In this action is rejected or failed.
- Settled: In this result is determined successfully, either fulfilled or rejected.
Syntax
const myPromise = new Promise((resolve, reject) => {
// Asynchronous operation
if (success) {
resolve(response);
} else {
reject(error);
}
});
myPromise.then(result => {
// Handle success here
}).catch(error => {
// Handle error here
});
Approach
In this approach, we will try to change the appeared data after some time without user intervention using promise. Here, we have an HTML code containing only data that is retrieved from a typescript file using String Interpolation. Now we have declared that data variable initially. Then using a Promise function, we have written a Promise which if in success changes the data variable to something else as below. So, as we have kept it as true, then the variable got changed below. This true and false may depend on various things in real-time projects. But for now, to demonstrate we keep it true and the time delay as 5 seconds.
Example: This example illustrates the basic usage of the Promise in Angular.
HTML
<!-- app.component.html -->
<div>
<p>{{ data }}</p>
</div>
JavaScript
// app.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
data: string = "data before promise response";
ngOnInit() {
// Asynchronous operation with a Promise
this.fetchDataWithPromise()
.then(data => {
this.data = data;
})
.catch(error => {
console.error(error);
});
}
fetchDataWithPromise(): Promise<string> {
return new Promise((resolve, reject) => {
setTimeout(() => {
// Data retrieved after some delay
const success = true;
// You can change this to false to simulate an error
if (success) {
resolve("Data fetched with a Promise"+
" and displayed after 5 seconds");
} else {
reject("Error occurred while fetching data");
}
}, 5000);
});
}
}
Output: Here data string is changed after the promise is retrieved.
 Disadvantages of Promises
- These promises are not cancellable in between the process. Once implemented, we must wait till a result is obtained. i.e., settled.
- These execute only once and don't repeat them again.
- Multiple values are not retrieved over time.
- When working on large applications, it's complicated.
Observable
An Observable is a powerful concept that is in Angular through the RxJS library. These are a sequence of values that can be arrived at over time. These can emit values either synchronously or asynchronously. Mainly used to handle streams of data that change over time. These can emit multiple values, unlike promises. These offer a great set of complex features such as cancellation, retrying, and debouncing. Suitable for real-time updates like stock market dashboards.
Syntax
import { Observable } from 'rxjs';
const myObservable = new Observable(observer => {
// Asynchronous operation
if (success) {
observer.next(result);
} else {
observer.error(error);
}
});
myObservable.subscribe(result => {
// Handle success here
}, error => {
// Handle error here
});
Example: This example describes the basic implementation of the Observable in Angular.
HTML
<!-- app.component.html -->
<div>
<p>{{ data }}</p>
</div>
JavaScript
// app.component.ts
import { Component, OnInit }
from '@angular/core';
import { Observable } from 'rxjs';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
data: string = "data before observable response";
ngOnInit() {
// Simulate an asynchronous operation
// with an Observable
this.fetchDataWithObservable().subscribe(
data => {
this.data = data;
},
error => {
console.error(error);
}
);
}
fetchDataWithObservable(): Observable<string> {
return new Observable(observer => {
setTimeout(() => {
// Data retrireving after some delay
const success = true;
// You can change this to false
// to simulate an error
if (success) {
observer.next("Data fetched with an Observable"
" and displayed after 5 sec");
observer.complete();
} else {
observer.error("Error occurred while fetching data");
}
}, 5000);
});
}
}
Output: Here is the output for the above example, with before and after observable responses.
 Disadvantages of Observables
- These are harder to learn than promises as it's from RxJS.
- Debugging is way harder when using Observables.
- Most times, we don't require what all observables offer.
Difference between Promise and Observable
 | Promise
| Observable
|
---|
Handling multiple values | Handles single value. | Handle multiple values at a time. |
---|
Asynchronous support | Suitable for asynchronous communication | Suitable for both synchronous and asynchronous communication |
---|
Cancellation | Cannot be canceled once initiated. | Can be canceled whenever we want. |
---|
Complex data transformation | Limited support. | Wide range of support. |
---|
Error Handling | The catch() method is used for handling errors. | This offers different mechanisms. |
---|
Conciseness | Simple and concise syntax. | More complex due to extensive support. |
---|
Use Cases | Suitable for one-time tasks like reading files. | Suitable for continuous real-time updates like in stock market dashboards. |
---|
Similar Reads
What is the difference between $watch and $observe in AngularJS ?
AngularJS provides different methods to observe/watch the changes in its elements and variables. The $observe and $watch are two different methods that serve this purpose. $observe is a method that is responsible to observe/watch the changes in the DOM attribute. We use $observe when we want to obse
3 min read
What is the Difference Between BehaviorSubject and Observable in Angular ?
Angular being a powerful framework offers various features and tools. One such powerful feature is to handle asynchronous operations by implementing the BehaviorSubject and Observable, which are the key components of the RxJS library. It also helps to manage the flow of data reactively. This article
5 min read
What is the Difference Between factory and service in AngularJS ?
AngularJS is a JavaScript-based framework. It can be used by adding it to an HTML page using a <script> tag. AngularJS helps in extending the HTML attributes with the help of directives and binding of data to the HTML with expressions. In this article, we will explore the differences between t
4 min read
What is the Difference Between $routeProvider and $stateProvider in AngularJS ?
In AngularJS, as we create Single-Page Applications, we need to implement the routing of components to view those images without having full page reloads. This can be achieved with the most popular routing methods, i.e., $routeProvider (ngRoute) and $stateProvider (ui-router).In this article, we wil
5 min read
What is the Difference Between required and ng-required in AngularJS ?
In web application development, AngularJS is one of the most favorite dynamic JavaScript frameworks that has HTML through script tags, enabling us to augment the HTML attributes using the directives and facilitating the data binding through the expressions.In AngularJS, we can use certain Directives
3 min read
What is the difference between Service Directive and Module in Angular ?
Angular is a Typescript framework used to build dynamic and Single-Page Applications. This has a strong focus on modularity and reusability of code which helps in creating complex and maintainable applications. At the core, Angular has 3 fundamental building blocks, i.e., Service, Directive and Modu
6 min read
What's the difference between ng-pristine and ng-dirty in AngularJS ?
AngularJS supports client-side form validation. AngularJS keeps track of all the form and input fields and it also stores the information about whether anyone has touched or modified the field or not. The two different classes ng-dirty and ng-pristine that are used for form validation, are described
2 min read
What is the difference between change and ngModelChange in Angular?
change: The change event is fired for <input>, <select>, and <textarea> elements when an alteration to the element's value is committed by the user.The change event is not necessarily fired for each alteration to an element's value. change is a DOM event that is it can trigger chan
2 min read
What is the difference between '@' and '=' in directive scope in AngularJS ?
AngularJS is a popular JavaScript framework, that provides powerful features for building dynamic web applications. When creating custom directives in AngularJS, you may come across the need to define a scope for your directive. The two most common methods to do this are by using the @ and = symbols
4 min read
What is the difference between â(â¦);â and â{â¦}â in ReactJS ?
When you write JavaScript, you can use either the "(â¦)" or "{â¦}" pattern to define objects. In ReactJS, (...); and {...} are used in different contexts and have different purposes, and are used to denote different types of code structures. What is "(â¦);" in React JS ?In ReactJS, (...); is used to de
5 min read