range
Creates an Observable that emits a sequence of numbers within a specified range.
Import
import { range } from 'rxjs';
Type Signature
function range(start: number, count?: number): Observable<number>;
Parameters
The value of the first integer in the sequence. If count is not provided, this becomes the count and start defaults to 0.
The number of sequential integers to generate.
Returns
An Observable that emits a finite range of sequential integers.
Description
range emits a sequence of sequential integers synchronously.
Examples
Basic Range
import { range } from 'rxjs';
range(1, 3).subscribe({
next: x => console.log(x),
complete: () => console.log('Complete!')
});
// Output:
// 1
// 2
// 3
// Complete!
Count Only
import { range } from 'rxjs';
// When only one argument, it's the count (starts from 0)
range(5).subscribe(x => console.log(x));
// Output:
// 0
// 1
// 2
// 3
// 4
Common Use Cases
Generate Test Data
import { range } from 'rxjs';
import { map } from 'rxjs/operators';
range(1, 100).pipe(
map(i => ({ id: i, name: `User ${i}` }))
).subscribe(user => console.log(user));
- of - Emit specific values
- generate - Generate with custom logic
- interval - Emit periodically
See Also