我使用mergeMap生成一批查询,以根据点找出地址。我将每个响应映射到数组中的对象。
//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生成的observable太早完成了。 "终于"在" nexts"中的一些(如果不是第一个)之后执行指令。在对Google的所有查询完成后,我该怎么做才能调用它?