Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Observable `of` deprecated. What is the equivalent?

In my case, I have an access token, and should that token exist, I would return it as an observable of type string:

if (this.accessToken){
  return of(this.accessToken);
}

Due to a recent update, I noticed that of is deprecated with the following message:

of is deprecated: use scheduled instead 'scheduled([a, b, c], scheduler)' (deprecation)

The new syntax is quite verbose, would anyone know the equivalent scheduled version of the same simple of? The keyword name makes it difficult to search for information on it.

Thanks!

like image 220
user1059939 Avatar asked May 14 '19 08:05

user1059939


People also ask

Is observable subscribe deprecated?

subscribe isn't deprecated, only the variant you're using is deprecated. In the future, subscribe will only take one argument: either the next handler (a function) or an observer object.

What is observable in JavaScript with example?

Observable JavaScript represents a progressive way of handling events, async the activity, and multiple values in JavaScript. These observables are just the functions that throw values and Objects known as observers subscribe to such values that define the callback functions such as error(), next() and complete().

What is of in RxJS?

RxJS' of() is a creational operator that allows you to create an RxJS Observable from a sequence of values. According to the official docs: of() converts the arguments to an observable sequence. For example: import { of } from 'rxjs'; of(1, 2, 3, 4) .

Is observable deprecated in Java 9?

Java's Observer and Observable Are Deprecated in JDK 9. Around since Java 1.0, the Observable class will be deprecated in Java 9, as is the Observer interface. See how they've been replaced over time. by. Dustin Marx.

Is it possible to deprecate the observable and observer classes?

Additional details justifying the deprecation of Observable and Observer can be found in JDK-8154801 ("deprecate Observer and Observable"). There is a quote in there from Josh Bloch as part of JDK-4180466 ("Why is java.util.Observable class not serializable.") dated February 1999: This class is no longer under active development.

What happened to observer and observable in JDK 9?

As per the post Java's Observer and Observable Are Deprecated in JDK 9 The event model supported by Observer and Observable is quite limited, the order of notifications delivered by Observable is unspecified, and state changes are not in one-for-one correspondence with notifications.

Why observer is not supported in Java 9?

Why Observer is deprecated in Java 9? Ans: The Observable class and the Observer interface have been deprecated in Java 9 because the event model supported by Observer and Observable is quite limited, the order of notifications delivered by Observable is unspecified, and state changes are not in one-for-one correspondence with notifications.


4 Answers

Only the overloads that accept a scheduler are deprecated. The variant that you are using is not deprecated, see https://github.com/ReactiveX/rxjs/blob/master/src/internal/observable/of.ts

like image 134
Daniel Hilgarth Avatar answered Oct 22 '22 23:10

Daniel Hilgarth


As said above, it is not deprecated.

I suppose you are migrating from RxJS v5 to RxJS v6. In that case:

The standard observable processing like of, map, filter, etc

Observable.of(1,2,3).map(x => 2 * x);

Becomes

import {of, map} from 'rxjs';
import {map} from 'rxjs/operators';

of(1,2,3).pipe(map(x => 2 * x));

Check more here https://www.learnrxjs.io/concepts/rxjs5-6.html

like image 27
hzitoun Avatar answered Oct 22 '22 21:10

hzitoun


If you do have a scheduler, the equivalent for of(item, scheduler) is scheduled([item], scheduler). If you're already passing in an array of items, you don't need the brackets.

like image 5
Sean Duggan Avatar answered Oct 22 '22 21:10

Sean Duggan


@daniel-hilgarth is right but you can use following commands if you need to emulate of(1, 2, 3)

import {asapScheduler, scheduled} from "rxjs";
scheduled([1, 2, 3], asapScheduler);

or

import {asyncScheduler, scheduled} from "rxjs";
scheduled([1, 2, 3], asyncScheduler);

You can read more about asap here: https://rxjs.dev/api/index/const/asapScheduler

like image 4
Matin Avatar answered Oct 22 '22 22:10

Matin