0

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('/'));
});
Mimetix
  • 272
  • 1
  • 4
  • 12

1 Answers1

0

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('/') });
Community
  • 1
  • 1
Paul
  • 139,544
  • 27
  • 275
  • 264