RXJS 重复查询直到满足条件? [英] RXJS Repeat query until a condition is met?
本文介绍了RXJS 重复查询直到满足条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用的 API 只返回有限数量的结果,比如 100.
I'm using an API that returns only a limited number of results, say 100.
我想重复一个查询,直到返回结果集为 <100
这意味着我得到了最后的结果.
I'd like to repeat a query until the return result set is < 100
which would mean I've got the last result.
所以它会是这样的:
- 进行查询
- 结果集是否小于限制?如果是,请再次执行并附加结果.
- 一旦结果集小于限制,发出最终结果.
推荐答案
您可以使用 expand 运算符来实现简单的条件重复"行为.
You can use the expand operator for a simple "conditional repeat" behavior.
仅作为示例,我将查询更改为返回一个数字,而不是结果集.下面继续查询,直到检索到的数小于100
Just for the example, instead of a result set I changed the query to return a number. The following keep querying until the retrieved number is less than 100
const { defer, empty } = rxjs;
const { expand, toArray} = rxjs.operators;
const query$ = defer(async () => Math.floor(Math.random()*1000));
query$
.pipe(
expand(result => result < 100 ? empty() : query$),
toArray()
)
.subscribe(console.log);
<script src="https://unpkg.com/rxjs/bundles/rxjs.umd.min.js"></script>
这篇关于RXJS 重复查询直到满足条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文