whatwg-fetch
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
Whatwg-fetch is a JavaScript package that provides the fetch function as a Promise-based mechanism for making web requests in the browser. It is a polyfill that implements a subset of the standard Fetch specification. It serves as a viable replacement for most uses of XMLHttpRequest in traditional web applications. However, it's worth noting that this package is for web browser use only and does not work in Node.js environments.
To use whatwg-fetch, you need to install the package using npm, then import it into your script where you want to leverage the fetch functionality. You can do this by running npm install whatwg-fetch --save
and then in your script file use import 'whatwg-fetch'
. Here is an example of how you can use the fetch function after importing the whatwg-fetch:
import 'whatwg-fetch'
fetch('/users.json')
.then(response => response.json())
.then(json => console.log('parsed json', json))
.catch(ex => console.log('parsing failed', ex))
In the above example, a fetch request is made to a JSON file, the response is converted to JSON, and then logged to the console. If the JSON fails to parse or the request fails due to network issues, an exception is caught and logged to the console.
It's important to note that fetch won't reject on HTTP error status, so handling for this scenario will need to be implemented manually.
The whatwg-fetch package documentation can be found directly in the package README file on the GitHub repository, which has the URL git+https://github.com/github/fetch.git. The documentation provides detailed explanation on how to use the package, including how to handle errors and caution points to be aware of.