Last active
February 23, 2019 00:44
-
-
Save TylerLH/88cea4d85656768803f26b7746ad8094 to your computer and use it in GitHub Desktop.
Multiple Async/Await in Express Route
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 express = require('express'); | |
const axios = require('axios'); | |
const app = express(); | |
async function getFoo() { | |
return Promise.resolve('foo'); // this could be anything that resolves | |
} | |
async function getBarData() { | |
try { | |
const data = await axios.get('https://jsonplaceholder.typicode.com/todos/') | |
return data; | |
} catch (err) { | |
throw err; | |
} | |
} | |
app.get('/', async (req, res) => { | |
const foo = await getFoo(); | |
const barData = await getBarData(); | |
return res.json({ foo, barData }); | |
}); | |
app.listen(process.env.PORT); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment