Created
June 26, 2024 08:23
-
-
Save SaltyAom/ef8696b7ff0a38e96a76cb90e4f91581 to your computer and use it in GitHub Desktop.
Bun.serve AbortSignal
This file contains hidden or 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
Bun.serve({ | |
port: 3000, | |
async fetch(request) { | |
request.signal.onabort = () => { | |
console.log('abort') | |
// ? Throwing an error will stop the whole process | |
// throw new Error('abort') | |
} | |
// Mock a delay (eg. database call) | |
await Bun.sleep(100) | |
console.log('Called') | |
return new Response('ok') | |
} | |
}) | |
fetch('http://localhost:3000', { | |
signal: AbortSignal.timeout(25) | |
}) | |
.then((x) => x.text()) | |
.catch(() => {}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment