Skip to content

Instantly share code, notes, and snippets.

@shairez
Created May 19, 2018 07:23
Show Gist options
  • Save shairez/f1f1a89ba1c4fa36b309eb260307e104 to your computer and use it in GitHub Desktop.
Save shairez/f1f1a89ba1c4fa36b309eb260307e104 to your computer and use it in GitHub Desktop.
Code examples for the switched-a-map blog post
// A FAKE SERVER OBJECT:
const http = {
getAwesomeMessagesObservable(name): Observable<string> {
return of(`${name} is awesome! (msg #1)`,
`${name} is awesome! (msg #2)`);
}
// the "of" returns an observable
// this particular one will produce 2 events
}
// FOR THE VALUE "Pete", ONCE WE SUBSCRIBE, THIS SHOULD RETURN:
// Pete is awesome! (msg #1)
// Pete is awesome! (msg #2)
// Awesome Mapping
const namesObservable = of('Pete', 'Mike');
namesObservable.pipe(
map(name => `${name} is awesome!`)
).subscribe(result => console.log(`${result}`));
// CONSOLE:
// Pete is awesome!
// Mike is awesome!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment