• Fetches data from a specified URL and returns it in the requested format.

    Parameters

    • dataLoc: string

      The URL or location to fetch data from.

    • Optionalformat: string = 'json'

      The format of the returned data ('json' or 'text').

    Returns Promise<any | string>

    • A promise that resolves to the fetched data: parsed JSON (if format is 'json') or raw text (if format is 'text').

    fetchData

    • Throws an error if the fetch fails or JSON decoding fails (for 'json' format).

    Retrieves data from a URL, parsing it as JSON by default or returning it as text if specified.

    // Fetching JSON data
    fetchData('https://api.example.com/data')
    .then(data => console.log(data))
    .catch(err => console.error(err));

    // Fetching text data
    fetchData('https://example.com/file.txt', 'text')
    .then(text => console.log(text))
    .catch(err => console.error(err));