Web Component prevent multiple API fetches I work on a small Web Component that converts an amount of money to another currency. For that, I use an API that returns the exchange rates.
I want to use the component multiple times on a page and thought that it would be a waste of traffic if every component fetches the same exchange rates. So I wrote some logic that only the first handled component fetches it, stores the results in a window variable. The other components notice that some already loads the data and store a callback in that window variable. When the fetching is done all callbacks will be resolved.
This works great but I wonder if this is a bad practice or whether this is even unnecessary because maybe it is cached etc.
Code (the described part is in the &quot;connectedCallback&quot; function):
const template = document.createElement('template');
template.innerHTML = `
  &lt;style&gt;
    .curr-shell {
      color: #262626;
      background: #F1F5F9;
      border-radius: 16px;
      padding: 4px 8px;
      font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, &quot;Helvetica Neue&quot;, Arial, &quot;Noto Sans&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;, &quot;Noto Color Emoji&quot;;
      box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1), 0 1px 2px 0 rgba(0, 0, 0, 0.06);
    }
  &lt;/style&gt;
  &lt;span class=&quot;curr-shell&quot;&gt;&lt;/span&gt;`;

class CurrencyConverter extends HTMLElement {
  constructor() {
    super();
    this.attachShadow({ mode: 'open' });
    this.shadowRoot.appendChild(template.content.cloneNode(true));
  }

  async fetchApi() {
    return new Promise((resolve, reject) =&gt; {
      fetch(`https://api.exchangeratesapi.io/latest?base=${this.baseCurrency}`)
        .then((response) =&gt; response.json())
        .then((data) =&gt; resolve(data))
        .catch((err) =&gt; reject(err));
    });
  }

  renderData() {
    const browserLocale = navigator.language;
    const originFormat = new Intl.NumberFormat(browserLocale, {
      style: 'currency',
      currency: this.baseCurrency,
    });
    const conversionFormat = new Intl.NumberFormat(browserLocale, {
      style: 'currency',
      currency: this.conversionCurrency,
    });

    const originAmount = originFormat.format(this.value);

    this.convertedAmount =
      this.value *
      window.rates[this.baseCurrency].data.rates[this.conversionCurrency];

    const conversionAmount = conversionFormat.format(this.convertedAmount);

    this.shadowRoot.querySelector(
      '.curr-shell'
    ).innerText = `${originAmount} | ${conversionAmount}`;
  }

  async connectedCallback() {
    this.baseCurrency = this.getAttribute('base-currency');
    this.value = parseFloat(this.getAttribute('value'));
    this.conversionCurrency = this.getAttribute('conversion-currency');

    if (!window.rates) window.rates = {};

    if (!window.rates[this.baseCurrency]) {
      // first one -&gt; fetch api
      window.rates[this.baseCurrency] = {
        status: 'fetching',
        data: null,
        callbacks: [],
      };

      window.rates[this.baseCurrency].data = await this.fetchApi();
      window.rates[this.baseCurrency].status = 'loaded';
      this.renderData();
      // resolve all callbacks from the waiting ones
      window.rates[this.baseCurrency].callbacks.forEach((cb) =&gt; cb());
    } else if (window.rates[this.baseCurrency].status === 'fetching') {
      // currently some else is fetching -&gt; add callback to be called when done
      window.rates[this.baseCurrency].callbacks.push(() =&gt; this.renderData());
    } else {
      // all data loaded
      this.renderData();
    }
  }
}

window.customElements.define('currency-converter', CurrencyConverter);

Demo: https://phartenfeller.github.io/currency-converter-wc/demo/
From a super short review;

I would attach the style tag to the head element, especially if you can have this element several times on the page (and ensure that you only add it once)

When all a fat arrow function does is call an another function with the provided parameter(s), then you might as well
  .then(resolve)
  .catch(reject);

instead of
  .then((data) =&gt; resolve(data))
  .catch((err) =&gt; reject(err));


This will not work in a rainy day scenario;
 window.rates[this.baseCurrency].data.rates[this.conversionCurrency];

Consider how you would handle this if window.rates[this.baseCurrency] is missing.