Created
March 11, 2022 00:08
-
-
Save beardedtim/671c8f56f9442d53792fb1465568d2cd to your computer and use it in GitHub Desktop.
Async Await Bites Again
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
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) | |
} | |
} |
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
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) | |
} | |
} |
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
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