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屋!
查看全文