node.js - Nodejs promises, launches function before previous one finished -


maybe not understanding promises, why 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 , passing return values then instead of passing functions themselves:

.then(console.log("guardado")) .then(res.redirect('/')); 

this same problem as described here.

you can use bind function argument bound:

.then(console.log.bind(console, "guardado")) .then(res.redirect.bind(res, '/')); 

you use anonymous functions, if don't of bind:

.then( function () { console.log("guardado") }) .then( function () { res.redirect('/') }); 

Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -