Skip to content

Instantly share code, notes, and snippets.

@seanwu1105
Last active November 27, 2020 07:21
Show Gist options
  • Save seanwu1105/3bbe4dc449be9ecd7ad70ab36d03ebff to your computer and use it in GitHub Desktop.
Save seanwu1105/3bbe4dc449be9ecd7ad70ab36d03ebff to your computer and use it in GitHub Desktop.
RxJS WTF

RxJS WTF

tap(_ => func()) != mapTo(func())

Assume func is a synchronized function. tap(_ => func()) will call the function lazily. However, mapTo(func()) will call the function eagerly.

forkJoin

  • forkJoin will not emit anything if the input is an empty array.
  • forkJoin only emit one value than complete.
  • Use zip when you want to emit more than one values.

toArray

  • toArray only works on source observable completed.
  • Use scan((acc, arr) => [...acc, ...arr], [] as T) to emit values without the source completing. However, this might cause duplicated elements in emitted values.

No Single in RxJS

You never know if an observable emits more than one value! Thus, I prefer use Promise as a "eager" replacement for Single.

finalize is NOT what you think

The finalize operator will not get invoked in the order you expected. It will be invoked after unsubscription and thus makes the test of the function inside finalize impossible. See this issue for more details.

Marble Test Fails with Promise

Marble test requires the observable to be synchronous. Thus, any observable from Promise, such as defer or from, will fail the test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment