简体   繁体   中英

What does Observable.from([true]) do?

Looking through my code base, I've noticed that a previous developer, when creating a function to transform one model to another, commonly uses the following code:

property: Observable.from([true]);

What does this accomplish exactly and why can't the value just be set to true ?

According to the documentation , the from operator:

Creates an Observable from an Array, an array-like object, a Promise, an iterable object, or an Observable-like object.

Why should we use the from operator if the array is only ever 1 value?

I am guessing, these are conventions followed by some teams or individuals, where

  1. members are initialised to determine type and initial/default value, here property is of type Observable<boolean> , which means this member data is calculated asynchronously (and is most likely to be used with an async pipe in the HTML incase of angular).
  2. for the sake of maintenance and understandability, one could use an array instead of a single value, just to indicate that this property emits multiple values instead of a single value. (Just informs developers to be aware of this during the design or implementation)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM