DEV Community

Philip Mutua
Philip Mutua

Posted on

2

What is the difference between Promises and Observables?

rxjs logo
Both observables and promises help us work with asynchronous functionality in JavaScript. Promises deal with one asynchronous event at a time, while observables handle a sequence of asynchronous events over a period of time.

Here are the differences in concept between Observables and Promises.

Observables Promises
Emit multiple values over a period of time. Emit a single value at a time.
Are lazy: they’re not executed until we subscribe to them using the subscribe() method. Are not lazy: execute immediately after creation.
Have subscriptions that are cancellable using the unsubscribe() method, which stops the listener from receiving further values. Are not cancellable.
Provide the map, filter, reduce, retry, retryWhen, and so many other RxJS operators, that makes it easy to deal with Observables. Don’t provide any operations.
Deliver errors to the subscribers. Push errors to the child promises.

code snippets/examples of a few operations defined by observables and promises.

Image description

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay