Effect CommunityEC
Effect Community3y ago
11 replies
shaugh

Comparing two approaches for geocoding in TypeScript

Which is more idiomatic:
export const isMapsRequestError = (
  err: unknown
): err is google.maps.MapsRequestError =>
  err instanceof google.maps.MapsRequestError;

export const geocode = (address: string) =>
  Effect.tryPromise({
    try: () => new window.google.maps.Geocoder().geocode({ address }),
    catch: (err) =>
      isMapsRequestError(err)
        ? err.code
        : google.maps.GeocoderStatus.UNKNOWN_ERROR,
  });

or:
export const geocode = (address: string) =>
  Effect.tryPromise({
    try: () => new window.google.maps.Geocoder().geocode({ address }),
    catch: (err) =>
      isMapsRequestError(err) ? Cause.fail(err) : Cause.die(err),
  });

or is there a more sensible way to do this?
Was this page helpful?