Friday, 17 January 2020

Chained promises question / problem

Hi guys,​I have written a small node.js application. I work with asynchronous queries and chained promises with bluebird. Apparently my implementation of the chained promise is not working. The last ".then" does not wait for the previous code to finish (see comments in the code below).Can anyone help?The following is a snipet of my code:const Promise = require('bluebird'); // creating database connection const connection = mysql.createConnection ({ host: 'localhost', user: '', password: '', database: '' }); // connect to database connection.connect((err) => { if (err) { throw err; } console.log('Connected to database'); }); // create promise var queryAsync = Promise.promisify(connection.query.bind(connection)); // getting query parameter for page var page = parseInt(req.query.page, 10) || 0; //calling promise queryAsync(sqlCount) .then(function(results) { numRows = results[0].numRows; numPages = Math.ceil(numRows / numPerPage); if (page >= numPages) { req.query.page = 0; } //this returns 0 console.log(req.query.page); }) .then(() => queryAsync(sql)) .then(function(results) { console.log(results); //this returns 2 console.log(req.query.page) next(); }) Thanks!!!

Submitted January 17, 2020 at 12:19PM by Stevelriemenbill

No comments:

Post a Comment