user1059939
user1059939

Reputation: 1715

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!

Upvotes: 53

Views: 27032

Answers (5)

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174369

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/packages/rxjs/src/internal/observable/of.ts

Upvotes: 40

Simple_tech
Simple_tech

Reputation: 49

You need to import correctly. I give the following example for your reference.

import { map } from 'rxjs/operators';

const httpOptions = {
headers: new HttpHeaders({
  'Content-type':  'application/json'
  })
};

return this.http.post(this.baseUrl + 'login', model, httpOptions).pipe(map((response : any) => {
const user = response.json();
if (user.accessToken){
  localStorage.setItem('token', user.accessToken);
  return user.accessToken;
} }))

Upvotes: -3

Sean Duggan
Sean Duggan

Reputation: 1135

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.

Upvotes: 5

hzitoun
hzitoun

Reputation: 5832

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

Upvotes: 15

Matin
Matin

Reputation: 71

@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

Upvotes: 6

Related Questions