What is @ardatan/sync-fetch?
@ardatan/sync-fetch is an npm package that provides a synchronous version of the fetch API. This can be particularly useful in environments where asynchronous operations are not feasible or desired, such as in certain testing scenarios or when working with legacy codebases.
What are @ardatan/sync-fetch's main functionalities?
Synchronous HTTP GET Request
This feature allows you to perform a synchronous HTTP GET request. The code sample demonstrates fetching a post from a placeholder API and logging the response data.
const fetch = require('@ardatan/sync-fetch');
const response = fetch('https://jsonplaceholder.typicode.com/posts/1');
const data = response.json();
console.log(data);
Synchronous HTTP POST Request
This feature allows you to perform a synchronous HTTP POST request. The code sample demonstrates sending a POST request to a placeholder API with a JSON payload and logging the response data.
const fetch = require('@ardatan/sync-fetch');
const response = fetch('https://jsonplaceholder.typicode.com/posts', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ title: 'foo', body: 'bar', userId: 1 })
});
const data = response.json();
console.log(data);
Handling HTTP Headers
This feature allows you to include HTTP headers in your synchronous fetch requests. The code sample demonstrates fetching a post with an Authorization header.
const fetch = require('@ardatan/sync-fetch');
const response = fetch('https://jsonplaceholder.typicode.com/posts/1', {
headers: {
'Authorization': 'Bearer token'
}
});
const data = response.json();
console.log(data);
Other packages similar to @ardatan/sync-fetch
sync-request
sync-request is another package that provides synchronous HTTP requests. It supports various HTTP methods and is often used in scenarios where asynchronous code is not suitable. Compared to @ardatan/sync-fetch, sync-request has a more extensive API but may be heavier in terms of dependencies.
urllib-sync
urllib-sync is a package that offers synchronous HTTP requests with a focus on simplicity and ease of use. It is similar to @ardatan/sync-fetch in that it provides a straightforward way to make synchronous HTTP requests, but it may lack some of the advanced features found in other packages.
sync-fetch
Synchronous wrapper around the Fetch API. Uses node-fetch
under the hood, and for some input-parsing code and test cases too.

Install
npm install sync-fetch
Use
const fetch = require('sync-fetch')
const metadata = fetch('https://doi.org/10.7717/peerj-cs.214', {
headers: {
Accept: 'application/vnd.citationstyles.csl+json'
}
}).json()
Limitations
Node.js
- Does not support
Stream
s (or FormData
) as input bodies since they cannot be read or serialized synchronously - Does not support
Blob
s as input bodies since they're too complex - Does not support the non-spec
agent
option as its value cannot be serialized
Browser
- Does not support most options, since
XMLHttpRequest
is pretty limited. Supported are:
method
body
headers
credentials
(but not omit
)- (Non-spec)
timeout
- Does not support binary responses in the main thread
- CORS limitations apply, of course (note they may be stricter for synchronous requests)