Skip to content

refactor: use fetch #32

@lishaduck

Description

@lishaduck

Description

This template uses both the fetch API and XMLHTTPRequest.
I believe fetch is faster and it's certainly less verbose (which is faster to load, at least).
I think it's worth doing mostly just for consistency though, as there's no reason to use XMLHTTPRequest + Promise.resolve if you only target environments supporting fetch.

Requirements

??

  • All CI/CD checks are passing.
  • There is no drop in the test coverage percentage.

Additional Context

I replaced it with this code, haven't extensively tested it though:

async function load(url) {
  try {
    const response = await fetch(url);
    if (!response.ok) {
      throw new Error(
        `Failed to load: ${response.status} ${response.statusText}`,
      );
    }
    return await response.text();
  } catch (error) {
    throw new Error("Network error");
  }
}

I also tweaked the loadPromises definition, but it doesn't really make a difference:

const loadPromises = urls.map(async (url) => {
  await load(url);
  await reportProgress();
});

I can PR, I just wanted to check if this was a change y'all agreed with, or if there was a reason for doing it this way in the first place.

Metadata

Metadata

Assignees

No one assigned

    Labels

    refactorA code change that neither fixes a bug nor add a feature

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions