Skip to content

Instantly share code, notes, and snippets.

@beardedtim
Created March 11, 2022 00:08
Show Gist options
  • Save beardedtim/671c8f56f9442d53792fb1465568d2cd to your computer and use it in GitHub Desktop.
Save beardedtim/671c8f56f9442d53792fb1465568d2cd to your computer and use it in GitHub Desktop.
Async Await Bites Again
const someDatabaseLogic: RequestHandler = async (req, res) => {
await setTimeout(1000)
res.end('Goodbye!')
}
export const someAPIHandler: RequestHandler = async (req, res, next) => {
try {
console.log('we are handling a requst')
return someDatabaseLogic(req, res, next)
} catch (e) {
console.log('An error was thrown trying to handle the request.')
console.log('Going to tell Express about this error so the client knows!')
return next(e)
}
}
const someSubHandler: RequestHandler = async (req, res) => {
await setTimeout(1000)
throw new Error('OH NO SOME DB ERROR FAILED!')
res.end('Goodbye!')
}
export const getPostById: RequestHandler = async (req, res, next) => {
try {
console.log('we are handling a requst')
await someSubHandler(req, res, next)
} catch (e) {
console.log('An error was thrown trying to handle the request.')
console.log('Going to tell Express about this error so the client knows!')
return next(e)
}
}
const someSubHandler: RequestHandler = async (req, res) => {
await setTimeout(1000)
throw new Error('OH NO SOME DB ERROR FAILED!')
}
export const getPostById: RequestHandler = async (req, res, next) => {
try {
console.log('we are handling a requst')
return someSubHandler(req, res, next)
} catch (e) {
console.log('An error was thrown trying to handle the request.')
console.log('Going to tell Express about this error so the client knows!')
return next(e)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment