Skip to content

Instantly share code, notes, and snippets.

@SKumarSpace
Created July 18, 2023 18:07
Show Gist options
  • Save SKumarSpace/60fe0abec16946635186dbc2adc5b998 to your computer and use it in GitHub Desktop.
Save SKumarSpace/60fe0abec16946635186dbc2adc5b998 to your computer and use it in GitHub Desktop.
cool fetch
function myCoolFetch(url: string): Promise<Response> {
// Simulating an asynchronous operation
return new Promise<Response>((resolve, reject) => {
// Simulating a delay before resolving the promise
setTimeout(() => {
// Create a mock response object
const response: Response = {
status: 200,
statusText: "OK",
ok: true,
redirected: false,
type: "basic",
url: url,
clone: () => response,
body: null,
bodyUsed: false,
blob: () => Promise.resolve(new Blob()),
formData: () => Promise.resolve(new FormData()),
arrayBuffer: () => Promise.resolve(new ArrayBuffer(0)),
headers: new Headers(),
// headers: {
// "Content-Type": "application/json",
// },
text: () =>
Promise.resolve(JSON.stringify({ message: "Hello, world!" })),
json: () => Promise.resolve({ message: "Hello, world!" }),
};
// Resolve the promise with the mock response
resolve(response);
}, 1000);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment