I have looked at this question/answer but can't get it to work for my needs.
I have an async
function in an offer.js
file, which I need to call from a Controller file. The offer.js
file works correctly and returns JSON data. It's the calling Controller file which I can't get to 'wait' for the data to come back before continuing with the rest of the page.
This is what the Controller file does:
var router = require('express').Router(); // Express.js router functionality
const Offer = require('../models/offer'); // the offer.js file which has getAllOffers() async function
// call/return the data from getAllOffers() async function
var rsOffersAll = async function() {
return await Offer.getAllOffers();
}
// I would like rsOffersAll to have data before doing anything further at this point.
// if the homepage, supply the rsOffersAll data to the view.
router.get('/', function(req, res, next) {
res.render('index', { data: rsOffersAll }); // the data needs to be here. this should not run until the data is available to pass to the view
});
How do I ensure that var rsOffersAll
has data before the router.get...
stuff executes?