Last active
August 29, 2015 14:19
-
-
Save tgroshon/cf3d02414cb74cd8721e to your computer and use it in GitHub Desktop.
Noodling with Promise usage.
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
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