From trawling through answers I realize this question gets asked a lot. For anyone like me that is new to this I recommend reading https://dev.to/ramonak/javascript-how-to-access-the-return-value-of-a-promise-object-1bck
I also read this: Async function returning promise, instead of value
Nevertheless despite all this I still can't get my code to work!
/* GET users listing. */
router.get('/', function(req, res, next) {
const message = test()
res.render('index', { title: ' Test: ' + test() });
});
function test()
{
const url = 'http://backend:5000/api'
fetch(url).then(response => {
console.log("This is working")
return response.text();})
.catch(error => {
console.log("This is not returning anything")
return "Error"});
}
This gives me a response of Test: undefined
. The console.log shows is printing "This is working" and I have separately tested the API that it is calling previously (it's a simple one I created in Flask that returns some text).