gpt4 book ai didi

javascript - RxJS 超时不起作用

转载 作者:行者123 更新时间:2023-11-28 18:06:33 25 4
gpt4 key购买 nike

当 Observable 流花费超过 3 秒时,我尝试退出它。问题是,当我多次复制并粘贴相同的值时,distinctUntilChanged 运算符不会让输入流通过。所以如果没有字符串流通过它,我想超时。这是我所拥有的。

        import { Subject } from "rxjs/Subject";
import "rxjs/add/operator/filter";
import "rxjs/add/operator/debounceTime";
import "rxjs/add/operator/distinctUntilChanged";
import "rxjs/add/operator/switchMap";
import "rxjs/add/operator/timeout";

this._searchSubject
.filter(val => val.length > 0)
.debounceTime(500)
.distinctUntilChanged()
.timeout(3000)
.switchMap(userSearchInput => {
...api call that returns Promise
})
.subscribe(searchResults => {
...do stuff with the result
});

最佳答案

超时会引发TimeoutError,您正在处理此错误吗?

Rx.Observable.from(new Promise(resolve => setTimeout(resolve, 1000)))
.timeout(500)
.subscribe(console.log, ({ message }) => console.error(message));

或者,您可以使用 timeoutWithRx.Observable.empty() 结束流:

Rx.Observable.from(new Promise(resolve => setTimeout(resolve, 1000)))
.timeoutWith(500, Rx.Observable.empty())
.subscribe(null, null, () => console.log('done'));

关于javascript - RxJS 超时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42446185/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com