10

I would like to create a delayed observable in typescript by:

import 'rxjs/add/observable/of';

...

const o = Observable.of(values).delay(10000);
o.subscribe((v) => { alert(v); });

but I got the following error:

"Observable_1.Observable.of(...).delay is not a function"
Dániel Kis
  • 2,341
  • 5
  • 28
  • 51

2 Answers2

15

In rxjs6 operators can be found in the 'rxjs/operators/*' packages.

import { delay } from 'rxjs/operators/delay';

of(values).pipe(
    delay(1000)
).subscribe(console.log);

In older versions can import the delay operator separately.

import 'rxjs/add/operator/delay';
toskv
  • 30,680
  • 7
  • 72
  • 74
  • In rxjs6, the of function is imported like this import { Observable, Subject, of } from 'rxjs';. And used like this: of(values) – yonexbat May 13 '18 at 19:56
  • yeah rxjs6 changed stuff. it would probably be easier to use pipeable operators from `'rxjs/operators/delay'`. I'll update the answer, thanks for pointing this out. – toskv May 13 '18 at 20:52
  • This is a RxJS 5 question. Your solution for version 6 is not helpful. – Peter Wippermann Jul 24 '18 at 10:10
  • 1
    In RxJS v6, `Observable.of()` becomes `of()`. See: https://github.com/ReactiveX/rxjs/blob/master/docs_app/content/guide/v6/migration.md#umd-module-name-change <- https://stackoverflow.com/a/38067643/3102325 – Marian07 Jan 23 '19 at 07:20
  • I find these docs to be the clearest: https://rxjs-dev.firebaseapp.com/ - https://rxjs-dev.firebaseapp.com/api/index/function/of – Marian07 Jan 23 '19 at 07:28
3

In rxjs 6 this works fine:

import { Observable, of  } from 'rxjs';
import { delay } from 'rxjs/operators';

...    

const o = of(values).pipe(
  delay(10000)
);

o.subscribe( v => alert(v) );
Francesco
  • 405
  • 3
  • 15