【发布时间】:2017-10-18 21:04:27
【问题描述】:
在 RxJs 中是否有一种方法可以执行两个 api 调用,其中第二个调用需要来自第一个的数据并将组合结果作为流返回?我想要做的是调用 facebook API 来获取组列表和各种大小的封面图像。 Facebook 返回如下内容:
// call to facebook /1234 to get the group 1234, cover object has an
// image in it, but only one size
{ id: '1234', cover: { id: '9999' } }
// call to facebook /9999 to get the image 9999 with an array
// with multiple sizes, omitted for simplicity
{ images: [ <image1>, <image2>, ... ] }
// desired result:
{ id: '1234', images: [ <image1>, <image2>, ... ] }
所以我有这个:
var result = undefined;
rxGroup = fbService.observe('/1234');
rxGroup.subscribe(group => {
rxImage = fbService.observe(`/${group.cover.id}`);
rxImage.subscribe(images => {
group.images = y;
result = group;
}
}
我想创建一个接受组 id 并返回一个 Observable 的方法,该方法将在流中包含组合的组 + 图像(此处为结果)。我知道我可以创建自己的 observable 并在其中调用 next() 函数,在那里我在上面设置了“结果”,但我认为必须有一种 rx 方式来做到这一点。 select/map 让我转换,但我不知道如何插入另一个电话的结果。 when/and/then 看起来很有希望,但看起来也不支持类似的东西。我可以map 并返回一个 observable,但调用者必须进行两次订阅。
【问题讨论】:
标签: rxjs