Skip to content

Instantly share code, notes, and snippets.

@SaltyAom
Created June 26, 2024 08:23
Show Gist options
  • Save SaltyAom/ef8696b7ff0a38e96a76cb90e4f91581 to your computer and use it in GitHub Desktop.
Save SaltyAom/ef8696b7ff0a38e96a76cb90e4f91581 to your computer and use it in GitHub Desktop.
Bun.serve AbortSignal
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