Skip to content
Discussion options

You must be logged in to vote

The beforeRequest hook can return a Response to skip the actual network request entirely. heres how to deduplicate parallel requests:

(untested)

const pendingRequests = new Map();

const api = ky.extend({
	hooks: {
		beforeRequest: [
			async request => {
				const key = `${request.method}:${request.url}`;
				
				// if theres already a pending request, return its promise
				if (pendingRequests.has(key)) {
					const response = await pendingRequests.get(key);
					// clone the response since it can only be read once
					return response.clone();
				}
				
				// otherwise, store the pending request promise
				const responsePromise = fetch(request).then(response => {
					pendingRequests.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by sindresorhus
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants