As I navigate my way through learning express, a question has arisen regarding the mechanics of the next() function.
- Is my understanding correct that when next() is invoked, it immediately initiates the execution of app.get, while anything below next() is carried out asynchronously?
- If this is indeed the case, why is the message 'Am I executed?' not displayed in the console when there is a significant delay set within the setTimeout() function?
I would appreciate a detailed explanation of the execution flow within the code provided below.
app.param('seriesId', (req, res, next) => {
... // Verify the presence of the series
console.log('I am executed');
next();
setTimeout(() => {console.log('Am I executed?')}, 1000); // Displays after 100ms, does not display after 1000ms
});
app.get('/:seriesId', (req, res, next) => {
... // Query the database to retrieve the series object
res.status(200).json({series: series});
});