I work on angular 9 i sarch more for different between promise and observable on angular 9
but i can't know what different
so can any one help me and tell me what different between both promise and observable and which is best for use ?
I work on angular 9 i sarch more for different between promise and observable on angular 9
but i can't know what different
so can any one help me and tell me what different between both promise and observable and which is best for use ?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Munesh SharmaRank #174 · 13.9K reputationPosted Feb 5, 2022, 3:53 AM EST
Use Promise instead of an Observable, when:
Use Observable instead of a Promise, when:
For more you can below link
https://www.youtube.com/watch?v=pzu17v2SeSM
Rajesh GamiRank #30 · 41.5K reputationPosted Feb 11, 2022, 5:15 AM EST
Promise
A
Promisehandles a single event when an async operation completes or fails.Note: There arePromiselibraries out there that support cancellation, but ES6Promisedoesn't so far.Observable
An
Observableis like aStream(in many languages) and allows to pass zero or more events where the callback is called for each event.Often
Observableis preferred overPromisebecause it provides the features ofPromiseand more. WithObservableit doesn't matter if you want to handle 0, 1, or multiple events. You can utilize the same API in each case.Observablealso has the advantage overPromiseto be cancellable. If the result of an HTTP request to a server or some other expensive async operation isn't needed anymore, theSubscriptionof anObservableallows to cancel the subscription, while aPromisewill eventually call the success or failed callback even when you don't need the notification or the result it provides anymore.While a
Promisestarts immediately, anObservableonly starts if you subscribe to it. This is why Observables are called lazy.Let’s see the difference between observable and promise (observable vs promise)
Now let’s see code snippets / examples of a few operations defined by observables and promises.
Srinivasan RamamoorthiRank #877 · 2.5K reputationPosted Oct 16, 2021, 8:16 AM EDT
Sachin SinghRank #66 · 26.2K reputationPosted Oct 16, 2021, 10:31 AM EDT
Rijwan AnsariRank #82 · 24.2K reputationPosted Oct 17, 2021, 2:57 AM EDT
Puneet KankarRank #446 · 5.6K reputationPosted Oct 17, 2021, 4:54 AM EDT
Hi Ahmed,
Below are the basic differences between observable and promises. check the below links for a better understanding.
https://stackoverflow.com/questions/37364973/what-is-the-difference-between-promises-and-observables
https://www.syncfusion.com/blogs/post/angular-promises-versus-observables.aspx
https://dzone.com/articles/what-is-the-difference-between-observable-and-prom
Satya KarkiRank #78 · 24.6K reputationPosted Oct 18, 2021, 6:46 AM EDT
Satya KarkiRank #78 · 24.6K reputationPosted Oct 18, 2021, 6:47 AM EDT
This diagram can give some more insights
satheesh d190 reputationPosted Nov 10, 2021, 6:31 AM EST
Yazhini MP128 reputationPosted Dec 3, 2021, 5:10 AM EST
Promises deal with one asynchronous event at a time, while observables handle a sequence of asynchronous events over a period of time. Emit multiple values over a period of time. Emit a single value at a time. Observables provide the map for forEach, filter, reduce, retry, and retryWhen operators. Promises doen't provide ant operations.
Munesh SharmaRank #174 · 13.9K reputationPosted Feb 12, 2022, 5:46 AM EST
Promise
A
Promisehandles a single event when an async operation completes or fails.Note: There are
Promiselibraries out there that support cancellation, but ES6Promisedoesn't so far.Observable
An
Observableis like aStream(in many languages) and allows to pass zero or more events where the callback is called for each event.Often
Observableis preferred overPromisebecause it provides the features ofPromiseand more. WithObservableit doesn't matter if you want to handle 0, 1, or multiple events. You can utilize the same API in each case.Observablealso has the advantage overPromiseto be cancellable. If the result of an HTTP request to a server or some other expensive async operation isn't needed anymore, theSubscriptionof anObservableallows to cancel the subscription, while aPromisewill eventually call the success or failed callback even when you don't need the notification or the result it provides anymore.you can find more from below link
https://www.youtube.com/watch?v=pzu17v2SeSM&t=61s
Amit Kumar SinghRank #1,016 · 1.9K reputationPosted Dec 29, 2023, 1:17 PM EST
In Angular 9 (and in JavaScript/TypeScript in general), Observables and Promises are both used for handling asynchronous operations, but they have some differences in their usage and behavior.
Promises:
.then()and.catch()to handle successful resolution or errors respectively.Example of a Promise in JavaScript:
const myPromise = new Promise((resolve, reject) => { // Perform asynchronous operation setTimeout(() => { // Simulate a successful result resolve('Data fetched successfully!'); // Simulate an error // reject('Error fetching data!'); }, 2000); }); myPromise .then((result) => { console.log(result); // Output: Data fetched successfully! }) .catch((error) => { console.error(error); // Output: Error fetching data! });Observables:
subscribe()to handle emitted values and other operators to transform, filter, or manipulate the stream of values.Example of an Observable in Angular using RxJS (a popular library for reactive programming):
import { Observable } from 'rxjs'; const myObservable = new Observable((observer) => { // Perform asynchronous operation setTimeout(() => { // Emit a value observer.next('Data fetched successfully!'); // Simulate an error observer.error('Error fetching data!'); }, 2000); }); const subscription = myObservable.subscribe( (value) => console.log(value), // Output: Data fetched successfully! (error) => console.error(error) // Output: Error fetching data! ); // To unsubscribe (cancel) the Observable s ubscription.unsubscribe();In Angular applications, Observables are widely used, especially with RxJS, because they provide powerful features for handling asynchronous data and are extensively used in services, HTTP requests, event handling, and more. Promises are also supported in Angular and can be used, but Observables are often preferred due to their flexibility and additional capabilities for handling streams of data.
Prasad RaveendranRank #320 · 7.8K reputationPosted Dec 29, 2023, 7:47 PM EST
In Angular 9 (and subsequent versions), both observables and promises are used for handling asynchronous operations, but they have different characteristics and functionalities.
Observables:
unsubscribe()method. This helps in managing resources and preventing memory leaks.Promises:
then()for handling a successful response andcatch()for handling errors.Summary:
In Angular, both observables and promises are used in different scenarios. Observables are extensively used with Angular's HttpClient for handling HTTP requests, managing event streams, and handling other asynchronous tasks. Promises are used when dealing with APIs that return promises or when integrating with existing promise-based libraries. However, Angular generally promotes the use of observables due to their flexibility and additional capabilities.