Skip to content

Instantly share code, notes, and snippets.

@TylerLH
Last active February 23, 2019 00:44
Show Gist options
  • Save TylerLH/88cea4d85656768803f26b7746ad8094 to your computer and use it in GitHub Desktop.
Save TylerLH/88cea4d85656768803f26b7746ad8094 to your computer and use it in GitHub Desktop.
Multiple Async/Await in Express Route
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