take(1) vs first()

June 2021

Are take(1) and first() the same? No. Learn what’s the difference.

take(1)

first

Example

  result = {};
  ngOnInit() {
    EMPTY.pipe(take(1)).subscribe(
      next => (this.result['take1'] = next),
      err => (this.result['take1'] = err)
    );
    EMPTY.pipe(first()).subscribe(
      next => (this.result['first'] = next),
      err => (this.result['first'] = err)
    );
  }

Result:

{
  "first": {
    "message": "no elements in sequence",
    "name": "EmptyError"
  }
}

Full example: https://stackblitz.com/edit/first-vs-take

Good SO answer regarding this: https://stackoverflow.com/a/42346203/3022127

Which one should you choose?

Here this SO answer is very useful: https://stackoverflow.com/a/54209901/3022127.

Dear Devs: You can help Ukraine🇺🇦. I opted for (a) this message and (b) a geo targeted message to Russians coming to this page. If you have a blog, you could do something similar, or you can link to a donations page. If you don't have one, you could think about launching a page with uncensored news and spread it on Russian forums or even Google Review. Or hack some russian servers. Get creative. #StandWithUkraine 🇺🇦
Dear russians🇷🇺. I am a peace loving person from Switzerland🇨🇭. It is without a doubt in my mind, that your president, Vladimir Putin, has started a war that causes death and suffering for Ukrainians🇺🇦 and Russians🇷🇺. Your media is full of lies. Lies about the casualties, about the intentions, about the "Nazi Regime" in Ukraine. Please help to mobilize your people against your leader. I know it's dangerous for you, but it must be done!