Skip to content

Calling the Loader twice when the API fails causes it falsely resolve the older promise #90

@louisnow

Description

@louisnow

Calling the Loader twice when the API fails or is blocked causes it to falsely resolve the older promise

This is common if you open another map in the same page.
For example, two map components calling the Loader shouldn't falsely trigger one of the promises to resolve, it must ideally throw an error.

To work around it, you could check if window.google in present in the .then
On a side note, this does not happen with https://www.npmjs.com/package/google-maps package, which from the README this library takes inspiration from and is what I'm currently using.

Environment details

  1. Specify the API at the beginning of the title (for example, "Places: ...")
  2. OS type and version : Linux
  3. Library version and other environment information: React

Steps to reproduce

  1. Block the maps.googleapis.com domain in your browser network tab.
  2. Call the loader twice or one after another with sufficient time in between
  3. One of the .then clauses will trigger.

Code example

    new Loader({ apiKey: process.env.GOOGLE_MAPS_API_KEY })
      .load()
      .then(() => {
        console.log('1 THEN');
      })
      .catch(() => {
        console.log('1 CATCH');
      });
    new Loader({ apiKey: process.env.GOOGLE_MAPS_API_KEY })
      .load()
      .then(() => {
        console.log('2 THEN');
      })
      .catch(() => {
        console.log('2 CATCH');
      });

This is especially noticeable when using it with React as fast-refresh or live-reload will remount the component during development and not rerender the page, triggering this bug even when you have a single map component

Metadata

Metadata

Assignees

Labels

priority: p0Highest priority. Critical issue. P0 implies highest priority.releasedtype: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions