From b8f9ee0e762a89f7866d216fa84797fcee4e2c4b Mon Sep 17 00:00:00 2001 From: Inga Date: Tue, 24 Oct 2023 01:07:42 +0000 Subject: [PATCH] cleaned rate limiting in osm --- src/clients/geocoding/osm.ts | 55 ++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/src/clients/geocoding/osm.ts b/src/clients/geocoding/osm.ts index deceb47..5941880 100644 --- a/src/clients/geocoding/osm.ts +++ b/src/clients/geocoding/osm.ts @@ -29,35 +29,36 @@ export const createOsmClient = (): Geocoder => { }); return { - geocode: async (query) => { - const result = await queue.add(() => geocoder.geocode(query)); + geocode: async (query) => + queue.add(async () => { + const result = await geocoder.geocode(query); - if (!result.length) { - throw new Error('No results found'); - } + if (!result.length) { + throw new Error('No results found'); + } - const meanLatitude = mean( - compact(result.map(({ latitude }) => latitude)), - ); - const meanLongitude = mean( - compact(result.map(({ longitude }) => longitude)), - ); - if ( - !result.every( - ({ latitude, longitude }) => - latitude && - longitude && - Math.abs(latitude - meanLatitude) < 0.01 && - Math.abs(longitude - meanLongitude) < 0.01, - ) - ) { - throw new Error('Ambiguous address'); - } + const meanLatitude = mean( + compact(result.map(({ latitude }) => latitude)), + ); + const meanLongitude = mean( + compact(result.map(({ longitude }) => longitude)), + ); + if ( + !result.every( + ({ latitude, longitude }) => + latitude && + longitude && + Math.abs(latitude - meanLatitude) < 0.01 && + Math.abs(longitude - meanLongitude) < 0.01, + ) + ) { + throw new Error('Ambiguous address'); + } - return { - latitude: meanLatitude, - longitude: meanLongitude, - }; - }, + return { + latitude: meanLatitude, + longitude: meanLongitude, + }; + }), }; };