简体   繁体   中英

RxJs use a subject to trigger a result in the current observable

In the code below I have to use setTimeout to get a value from this.parts$(def) because it's chained to an observable that listens to the action event. How can I get around this so I don't need to use a timeout to trigger a result from this.parts$(def)?

getParts$ = (def: Def) => {
        setTimeout(() => this.action.next('test'), 1);
        return this.parts$(def);
    };

Edit: Action defined as:

private action = new Subject<string>();

I guess what you to do is to subscribe to parts$ before action fire. You can try the below pattern

getParts$ = (def: Def) => {
      return new Observable(obs=>{
        const sub=this.parts$(def).subscribe(obs);
        this.action.next('test'), 1);
        return ()=>sub.unsubscribe()
      }
    };

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