Skip to content

Instantly share code, notes, and snippets.

@tgroshon
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tgroshon/cf3d02414cb74cd8721e to your computer and use it in GitHub Desktop.
Save tgroshon/cf3d02414cb74cd8721e to your computer and use it in GitHub Desktop.
Noodling with Promise usage.
async function asyncRead(inputId) {
var id = await inputId // Awaiting here makes function usable with sync or async inputs
// Do some async action that returns a promise
return Promise.resolve({id})
}
async function asyncWrite(inputModel) {
var model = await inputModel
// Do some async action that returns a promise
return Promise.resolve()
}
// Usage
//
async function endpoint(req, res, next) {
try {
var id = req.params.id // sync input
var model = await asyncRead(id)
var result = await asyncWrite(model) // async (Promise) input
res.send(201)
} catch (e) {
res.status(500).json({error: e.message})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment