Skip to content

Commit

Permalink
Fix a timing issue for sequential importLibrary calls
Browse files Browse the repository at this point in the history
Works around googlemaps#809
  • Loading branch information
chrisjshull authored Apr 16, 2024
1 parent 959ea63 commit 79694a9
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,9 +469,11 @@ export class Loader {
name: "visualization"
): Promise<google.maps.VisualizationLibrary>;
public importLibrary(name: Library): Promise<unknown>;
public importLibrary(name: Library): Promise<unknown> {
public async importLibrary(name: Library): Promise<unknown> {
this.execute();
return google.maps.importLibrary(name);
// Extra await here works around https://github.com/googlemaps/js-api-loader/issues/809.
// This is kind of an icky timing workaround, but the issue really is that user code after importLibrary is getting invoked 1 microtask before the library code handles the completion.
return await google.maps.importLibrary(name);
}

/**
Expand Down Expand Up @@ -629,7 +631,7 @@ export class Loader {
// short circuit and warn if google.maps is already loaded
if (window.google && window.google.maps && window.google.maps.version) {
console.warn(
"Google Maps already loaded outside @googlemaps/js-api-loader." +
"Google Maps already loaded outside @googlemaps/js-api-loader. " +
"This may result in undesirable behavior as options and script parameters may not match."
);
this.callback();
Expand Down

0 comments on commit 79694a9

Please sign in to comment.