I am having a hard time wrapping my head around what next is doing in this Express.js example and why it is used here.
This is the file where I am handling my routes.
const express = require('express');
const controller = require('../controllers/myappcontroller');
const myroutes = express.Router();
const apphelper = require('../services/appservices/apphelper');
myroutes.get('/', apphelper.mycoolfunction, controller.index);
This is the contents of the apphelper.js file
require('isomorphic-fetch');
function mycoolfunction(req, res, next) {
fetch('someurl')
.then((fetchRes) => {
return fetchRes.json();
}).then((jsonFetchRes) => {
res.locals.firstname = jsonFetchRes.contents.firstname[0].firstname;
next();
}).catch((err) => {
console.log(err);
res.locals.firstname = 'not available';
next();
});
}
module.exports = {
mycoolfunction: mycoolfunction,
};
Why is there a "next" in the parameter and why are they used in the function? Is it there so that