maybe i am not understanding promises, why am i getting redirected before functions.dioNic ends?
app.get('/dionic/scan', function(req, res) {
var functions = require("./functions.js");
var scrape = require("./scrapers/dionic.js");
//render index.ejs file
scrape.scrapeDio
// Remove duplicates in array data
.then(dedupe)
// Save
.then(functions.dioNic)
.then(console.log("Guardado"))
.then(res.redirect('/'));
});
You're calling these functions and passing their return values to then
instead of passing the functions themselves:
.then(console.log("Guardado"))
.then(res.redirect('/'));
This is effectively the same problem as described here.
You can use bind to get a function with the argument bound:
.then(console.log.bind(console, "Guardado"))
.then(res.redirect.bind(res, '/'));
You could also use anonymous functions, if you don't like the look of bind:
.then( function () { console.log("Guardado") })
.then( function () { res.redirect('/') });