throw

signature: throw(error: any, scheduler: Scheduler): Observable

Emit error on subscription.

throw - 图1

Examples

Example 1: Throw error on subscription

( jsBin |
jsFiddle )

  1. import { _throw } from 'rxjs/observable/throw';
  2. //emits an error with specified value on subscription
  3. const source = _throw('This is an error!');
  4. //output: 'Error: This is an error!'
  5. const subscribe = source.subscribe({
  6. next: val => console.log(val),
  7. complete: () => console.log('Complete!'),
  8. error: val => console.log(`Error: ${val}`)
  9. });

Additional Resources


:file_folder: Source Code:
https://github.com/ReactiveX/rxjs/blob/master/src/internal/observable/throwError.ts