Use AsyncPipe When Possible

I typically review a fair amount of Angular code at work. One thing I typically encourage is using plain Observables in an Angular Component, and using AsyncPipe (foo | async) from the template html to handle subscription, rather than directly subscribing to an observable in a component TS file.

Subscribing in Components

Unless you know a subscription you’re starting in a component is very finite (e.g. an HTTP request with no retry logic, etc), subscriptions you make in a Component must:

  1. Be closed, stopped, or cancelled when exiting a component (e.g. when navigating away from a page),
  2. Only be opened (subscribed) when a component is actually loaded/visible (i.e. in ngOnInit rather than in a constructor).


 export class Foo
   implements OnInit, OnDestroy {

   someStringToDisplay = '';
   private readonly onDestroy =
     new ReplaySubject<void>(1);

   ngOnInit() {
       map( ... ),
     ).subscribe(next => {
       this.someStringToDisplay = next;

   ngOnDestroy() {;
 export class Foo
   implements OnInit, OnDestroy {

   someStringToDisplay = '';
   private subscription =

   ngOnInit() {
     this.subscription = someObservable.pipe(
       map( ... ),
     ).subscribe(next => {
       this.someStringToDisplay = next;

   ngOnDestroy() {

AsyncPipe can take care of that for you

@Component() export class Foo {
   someStringToDisplay = someObservable.pipe(
<span>{{someStringToDisplay | async}}</span>

Much better! No need to remember to manage unsubscribe. No need to implement OnDestroyAsyncPipe does its own unsubscribe on destruction, etc. If you only implement OnInit to make a new subscription, you can forego that too.

Best Practice: Use publishReplay and refCount if accessing the same Observable from multiple places

If you need to access a value multiple times, consider using the publishReplay and refCount RxJS operators:

readonly pageTitle = this.route.params.pipe(
   map(params => params['id']),
   flatMap(id => this.http.get(
     {'responseType': 'text'})),
<h1>{{pageTitle | async}}</h1> 
<p>You are viewing {{pageTitle | async}}.</p>

This will cause the template rendering to make a single request for pageTitle, and cache the result between both uses.

Best Practice: Combine *ngIf, as, and else with AsyncPipe

If you need to handle the loading state, and need to display nested properties of an object returned from an observable, you can do something like:

<ng-container *ngIf="(pageObservable | async) as page; else loading">
   <!-- can refer to 'page' here -->
 <ng-template #loading>Loading…</ng-template>

Note that this doesn’t distinguish between the case where pageObservable is still loading, and the case where pageObservable resolved to a falsey value.

What, other than hypocrisy?

Close to half a million Syrians have died in the Syrian Civil war since 2011. In Aleppo alone, since 2012, over 100,000 Syrians have been killed. As of 2015, the UN puts the estimate of civilians killed by the Syrian regime at 250,000. Other estimates range from 150,960 to 470,000.[1]

More Arabs have been killed by Bashar Al-Assad since 2011 than by Israel since 1948.

More Arabs have been killed by Bashar Al-Assad since 2011 than by the US Iraqi Invasion (by most estimates).

More Arabs have been killed by Bashar Al-Assad since 2011 than by ISIS.[2]

War is ugly. We learn daily about atrocities committed by all players in the region. Certainly many of them by the secular rebels like the Free Syrian Army. Many of them by the U.S.-led coalition.

We all need to be ashamed. No one is on the “good” side here.

The regime deserves a special place, though.

What, other than hypocrisy allows one to protest Israel’s occupation yet excuse the regime? What, other than hypocrisy allows one to protest US Imperialism, yet excusing Russian and Iranian imperialism?

Perhaps it is denialism. We live in a world where basic facts are contested. Where there is no common truth. Perhaps it is not hypocrisy. Perhaps we each have our own realities, with their own numbers, and each reality continues to fuel our fight against our neighbors.

[1] Totals for war via SCPR and SOHR. Estimate for those killed by Syrian Regime via Quora answer, see answer for cited UNHCR reports.
[2] Estimates are in the mid-tens of thousands

A fifteen year-old lesson from New York

Reading Gloria Steinem’s My Life on the Road gave me many treasured lessons. Many are relevant on election years. One is especially relevant on the heels of the 15th anniversary of the 9/11 terrorist attacks, and particularly during an election year where fear and Islamophobia are on the ballot.
As a large portion of Americans react to fear of terrorism and religious-inspired radicalism with Islamophobia and an anti-immigrant mentality, I remember this.

Steinem recalls a conversation with a cab driver in New York city “only ten days after the 9/11 terrorist attacks”. “Downtown streets were covered with surrealistic gray ash and debris,” she says, “and gutters were filled with the bodies of birds that had been incinerated in flight.”
But then she rides in this man’s cab.

My driver was a quiet young white guy with a gravity that I sensed as soon as I got into his cab. We drove past construction fences covered with photos and notices posted by people who were still searching for missing relatives or friends or coworkers. There were also anonymous graffiti that had appeared as if by contagion all over New York with the same message: Our grief is not a cry of war.

“That’s how New Yorkers feel,” the driver said. “They know what bombing looks like, and they know the hell it is. But outside New York, people will feel guilty because they weren’t here. They’ll be yelling for revenge out of guilt and ignorance. Sure, we all want to catch the criminals, but only people who weren’t in New York will want to bomb another country and repeat what happened here.”

“He was right,” she says, “Even before it was clear that Iraq and Saddam Hussein had nothing to do with 9/11 […] 75 percent of New Yorkers opposed the U.S. bombing of Iraq. But a national majority supported it.”

Former New York Mayor Rudy Giuliani made a (widely misreported) claim that no major terror attack struck the U.S. after the passage of the PATRIOT Act. Giuliani is making the implication that the empowerment of the surveillance state, especially on Muslims, was related to a failure of major terror attacks to take place after 9/11 on U.S. soil. Perhaps it was related (there is mixed evidence on whether the PATRIOT act worked). Also related, perhaps, is George W. Bush’s refusal to empower Islamophobic rhetoric, Instead stating, less than a week after 9/11, “Islam is Peace.” Perhaps, I add, that a compassionate, understanding state is more equipped to handle terror threats, to empower communities to self-monitor and self-report, to empower communities to teach, affirm, and re-affirm peace, when it refuses to paint these communities with a broad brush.

Excerpts in this post are taken from Gloria Steinem’s My Life on the Road, pp. 72–3, Chapter III. The chapter is entitled “Why I Don’t Drive”. It describes insightful and perspective-changing experiences Steinem had by talking to cab drivers and public transit passengers over years of traveling around the world.

Reconsider! Are College Students Really Demanding ‘the Right to be Comfortable’?

Originally published on Medium, see Reconsider! here.

There is an empathy gap with how many academics and others view the issues happening on college campuses today.

Take a step back and notice that many participants of these movements are people of color, especially women of color, trans and gender-non-conforming people, etc. Their experience is not your experience. Sometimes, in an attempt to be empathetic, we try to rephrase or pigeon-hole another person’s experiences in terms that we understand. We think of times we’ve been ‘uncomfortable’, feel like those times were times of growth, and dismiss the younger generation’s demand as over-sensitive.

Consider that the types of ‘discomfort’ (as detractors call it) are types that you have never experienced, and occur in contexts you have never been through.

A section of our students today feel invisible, invalid, unheard. They see that people who do not have their experiences are likely to dismiss them. They are demanding some kind of acknowledgement and some kind of consideration. The answer is not necessarily a “coddling” of the American mind, Rani Neutill, a female professor of color, describes her failed attempt at using trigger warnings in the classroom.

The answer to the premise is still in flux. This is a topic of debate in which everyone is welcome to contribute. A lot of people, however, seem to reject the premise. I wish you would reconsider at least once before doing so.

Things to consider:

– with large movements of anything, you will see a mob mentality; I need not condone specific inappropriate behaviors to agree with their demands — I do not think they don’t invalidate their cause.
– look up “In-group favoritism” and “Out-group homogeneity” — there is a studied empathy gap with groups we whose experiences we are outsiders to. Tread carefully, before suggesting an out-group member is being oversensitive.

This is not about one e-mail, as Aaron Lewis writes in “What’s Really Going On at Yale”. Many students feel invalidated and unheard, and view the university’s response as silencing and invalidating. They want to be heard and they want answers. The solution is not yet settled! Do not yell “censorship!” and deny the existence of the problem. Join the conversation: after reading one article on The Atlantic about coddling, check out first-hand the perspectives of those protesting, for example, by reading something at DOWN at Yale.

Comment on Medium

The Evolution of Bernie Sanders on Race

Bernie Sanders flip-flopped too, and that’s okay. People keep mentioning Hillary Clinton’s shifting on issues as a mark against her, but as she explains, this is only evidence that she is a person who responds to new information and develops their opinion, not a block of granite. Sanders, too, has shifted left on race issues and gun control in the past several months. This is a wonderful thing.

I wrote on Medium about the Evolution of Bernie Sanders on Race. Please read the full post there.

I conclude:

When standing in solidarity with others in a diverse, progressive movement, the most important thing you can do is avoid subconsciously projecting your own privilege and predicament, onto others. Diversity is not about erasing differences, it is about embracing them.

Check it out on Medium to see how I come to that conclusion.

Picture by AFGE. Via Flickr. Licensed under CC-by-2.0

%d bloggers like this: