首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >mergeMap完成得太早

mergeMap完成得太早
EN

Stack Overflow用户
提问于 2017-09-12 15:26:33
回答 1查看 178关注 0票数 1

我使用一个mergeMap来产生一批查询,根据点查找地址。我将每个响应映射到数组中的对象。

代码语言:javascript
复制
//ptosDistintos = ['-34.5466 58.4363', '-34.5523 58.4486', ...]
this.suscCalcularDirecciones = Observable.from(ptosDistintos)

    .mergeMap(pos => {

        //I convert every position to a latLng object (Leaflet)
        const [lat, lng] = pos.split(' ');
        const latlng = L.latLng(+lat, +lng);

        //I make requests to Google to figure out addresses
        return this.http.get(`https://maps.googleapis.com/maps/api/geocode/json?latlng=${latlng.lat},${latlng.lng}`)
            .filter(x => x.length)
            //I return the address to the mergeMap.
            .map(direccion =>
                [pos, direccion.length ? direccion[0].formatted_address : '(no encontrada)'];
            );
    })
    .finally(() => {

        //I'm deleting the suscription once it is complete because in template I check for completion by checking emptyness of the subscription.
        delete this.suscCalcularDirecciones;
    })
    .subscribe(posDir => {
        const [pos, dir] = posDir;
        for (let i of agrup[pos].indexes) {
            this.historico[i].direccion = dir;
        }
        this.historico = this.historico.slice();
    });

然而,mergeMap所产生的可观察到的结果还为时过早。"finally“指令是在"nexts”的几个(如果不是第一个)之后执行的。在谷歌的所有查询完成后,我该怎么称呼它呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-13 21:51:43

最后,这是Google的一个问题。

代码语言:javascript
复制
.filter(x => x.length)

这一行删除了来自google的0结果的响应,因此合并完成时出现了很多失败。这是因为Google不允许每秒发出这么多请求(我同时发出了所有请求,有时有数百个请求)。

我在可观察到的地方添加了一个计时器,这样它就会随着时间的推移而发出请求。

代码语言:javascript
复制
Observable.timer(0, 100)
    .map(i=>ptosDistintos[i])
    .take(ptosDistintos.length)
    .mergeMap(...

希望这能帮上忙。我将添加google标签。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46180275

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档