StartWith

emit a specified sequence of items before beginning to emit the items from the source Observable

If you want an Observable to emit a specific sequence of items before it begins emitting the items normally expected from it, apply the StartWith operator to it.

(If, on the other hand, you want to append a sequence of items to the end of those normally emitted by an Observable, you want the Concat operator.)

See Also

Language-Specific Information:

cons

RxClojure implements this operator as cons. It takes an item and an Observable as parameters, and prepends the item to the items emitted by the Observable as its own Observable sequence.

start_with

RxCpp implements this operator as start_with. It takes an Observable and one or more items as parameters, and prepends these items, in the order they are given in the parameter list, to the items emitted by the Observable as its own Observable sequence.

RxGroovy implements this operator as startWith.

startWith

You can pass the values you want to prepend to the Observable sequence to startWith either in a single Iterable or as a series of one to nine function parameters.

Sample Code

def myObservable = Observable.from([1, 2, 3]);

myObservable.startWith(-3, -2, -1, 0).subscribe(
  { println(it); },                          // onNext
  { println("Error: " + it.getMessage()); }, // onError
  { println("Sequence complete"); }          // onCompleted
);
-3
-2
-1
0
1
2
3
startWith

You can also pass startWith an Observable, and it will prepend the emissions from that Observable to those of the source Observable to make its own set of emissions. This is a sort of inverted Concat operation.

RxJava implements this operator as startWith.

startWith

You can pass the values you want to prepend to the Observable sequence to startWith either in a single Iterable or as a series of one to nine function parameters.

startWith

You can also pass startWith an Observable, and it will prepend the emissions from that Observable to those of the source Observable to make its own set of emissions. This is a sort of inverted Concat operation.

startWith

RxJS implements this operator as startWith. It accepts a variable number of function parameters and treats each one as an item that it will prepend to the resulting Observable sequence in the order they are given in the parameter list.

startWith is found in the following distributions:

  • rx.js
  • rx.all.js
  • rx.all.compat.js
  • rx.compat.js
  • rx.lite.js
  • rx.lite.compat.js

RxKotlin implements this operator as startWith.

startWith

You can pass the values you want to prepend to the Observable sequence to startWith either in a single Iterable or as a series of one to nine function parameters.

startWith

You can also pass startWith an Observable, and it will prepend the emissions from that Observable to those of the source Observable to make its own set of emissions. This is a sort of inverted Concat operation.

StartWith

Rx.NET implements this operator as StartWith. It accepts an array of items which it prepends to the resulting Observable sequence in the order they appear in the array before it emits the items from the source Observable.

RxPHP implements this operator as startWith.

Prepends a value to an observable sequence with an argument of a signal value to prepend.

Sample Code

//from https://github.com/ReactiveX/RxPHP/blob/master/demo/startWith/startWith.php

$source = \Rx\Observable::of(2)
    ->startWith(1);

$subscription = $source->subscribe($stdoutObserver);

   
Next value: 1
Next value: 2
Complete!
    

RxPHP also has an operator startWithArray.

Prepends a sequence of values to an observable sequence with an argument of an array of values to prepend.

Sample Code

//from https://github.com/ReactiveX/RxPHP/blob/master/demo/startWith/startWithArray.php

$source = \Rx\Observable::of(4)
    ->startWithArray([1, 2, 3]);

$subscription = $source->subscribe($stdoutObserver);

   
Next value: 1
Next value: 2
Next value: 3
Next value: 4
Complete!
    
start_with

RxPY implements this operator as start_with. It accepts an array of items which it prepends to the resulting Observable sequence in the order they appear in the array before it emits the items from the source Observable.

start_with

Rx.rb implements this operator as start_with. It accepts an array of items which it prepends to the resulting Observable sequence in the order they appear in the array before it emits the items from the source Observable.

plus colon

RxScala implements this operator with +: It takes an item and an Observable as parameters, and prepends the item to the items emitted by the Observable as its own Observable sequence.