take(1) vs first()

June 2021

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

take(1)

first

Quick interlude
Tax Wealth, Not Work
Your salary is taxed on every payslip; large fortunes mostly aren't. That gap is a big part of why housing feels out of reach. Gary Stevenson breaks it down better than anyone: watch on YouTube →

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.

Tax Wealth, Not Work
Wages get taxed to the bone while the ultra-rich watch their assets compound untouched — and then buy up the houses you're saving for. If you work for your money, this is your fight too.
Watch on YouTube →