Skip to content

Latest commit

 

History

History
65 lines (51 loc) · 2.58 KB

fromevent.md

File metadata and controls

65 lines (51 loc) · 2.58 KB

fromEvent

signature: fromEvent(target: EventTargetLike, eventName: string, selector: function): Observable

Turn event into observable sequence.

Ultimate RxJS

Examples

Example 1: Observable from mouse clicks

( StackBlitz | jsBin | jsFiddle )

// RxJS v6+
import { fromEvent } from 'rxjs';
import { map } from 'rxjs/operators';

//create observable that emits click events
const source = fromEvent(document, 'click');
//map to string with given event timestamp
const example = source.pipe(map(event => `Event time: ${event.timeStamp}`));
//output (example): 'Event time: 7276.390000000001'
const subscribe = example.subscribe(val => console.log(val));

Related Recipes

Additional Resources


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