Last active
April 24, 2023 14:22
-
-
Save yoeven/5057e9579149a952516457ee2c3a4809 to your computer and use it in GitHub Desktop.
Fetch without a response (JS)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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