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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Prasad RaveendranPosted Dec 30, 2023, 12:47 AM
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.
Amit Kumar SinghPosted Dec 29, 2023, 6:17 PM
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:
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):
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.
Munesh SharmaPosted Feb 12, 2022, 10:46 AM
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
Rajesh GamiPosted Feb 11, 2022, 10:15 AM
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.
Munesh SharmaPosted Feb 5, 2022, 8:53 AM
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
Yazhini MPPosted Dec 3, 2021, 10:10 AM
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.
satheesh dPosted Nov 10, 2021, 11:31 AM
Satya KarkiPosted Oct 18, 2021, 10:47 AM
This diagram can give some more insights
Satya KarkiPosted Oct 18, 2021, 10:46 AM
Puneet KankarPosted Oct 17, 2021, 8:54 AM
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
Rijwan AnsariPosted Oct 17, 2021, 6:57 AM
Sachin SinghPosted Oct 16, 2021, 2:31 PM
Srinivasan RamamoorthiPosted Oct 16, 2021, 12:16 PM