Skip to content

Instantly share code, notes, and snippets.

@yoeven
Last active April 24, 2023 14:22
Show Gist options
  • Save yoeven/5057e9579149a952516457ee2c3a4809 to your computer and use it in GitHub Desktop.
Save yoeven/5057e9579149a952516457ee2c3a4809 to your computer and use it in GitHub Desktop.
Fetch without a response (JS)
//https://www.sensedeep.com/blog/posts/stories/lambda-fast-http.html
export const fetchWithoutResp = async (
url: string,
body?: string | Buffer,
headers: {
[key: string]: string;
} = {},
method: "POST" | "GET" = "POST"
) => {
const router = url.includes("http://") ? http : https;
return new Promise((resolve, reject) => {
const req = router.request(url, {
method: method,
headers: {
"Content-Type": "application/json",
"Content-Length": body?.length || 0,
...headers,
},
});
body && req.write(body);
req.end(null, () => {
/* Request has been fully sent */
resolve(req);
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment