I am trying to create a route in Node.js / Express that reads data from two requests and then increases the score based on this data from countries. Since Node.js is asynchronous, my total amount is displayed before all data is read.
I created a simple example that goes as far as what I'm doing right now
var express = require('express');
var router = express.Router();
var total = 0;
router.get('/', function(req, res, next) {
increment(3);
increment(2);
console.log(total);
res.end();
});
var increment = function(n){
setTimeout(function(){
for(i = 0; i < n; i++){
total++;
}
}, n *1000);
};
module.exports = router;
I'm not sure what I will need to do to wait until both functions finish before I print the result. Should I create a custom Event Emitter to achieve this?
source
share