0

I am using the following code in Node.js to call a database query using a promise. That part works fine. I am attempting to 'pause' my script execution until the database query completes...that is the part causing a problem.

my code:

var _startup = 1;  //toggle to check database on web server reboots...
var isPaused = 1;  //toggle to 'delay' database call function...
const db = require('./routes/queries');

app.get('/lookup', function (req, res) {

 if (_startup === 1) {
 isPaused = 1;
 _line_update();  //create/update member status arrays...

  //'delay' until '_line_update()' has completed...
  function waitForIt(){
   if (isPaused === 1) {
   setTimeout(function(){waitForIt()},100);
   } else {
   console.log("DATABASE CALL COMPLETED...");
   console.log(lineup);  
   console.log(picture);
   console.log(present);
   };
  }

 _startup = 0;  //toggle 'off'
 }

});

function _line_update() {
lineup = []; 
picture = []; 
present = [];  //create an 'empty' array EACH CALL...  

 //**Query PostGres database...using a promise

 db.getMembers()
 .then(function(value) {
 console.log('Async success!', value);
 console.log('length of value passed from database query: ' + value.length);

 //...DO STUFF HERE TO POPULATE ARRAYS ('lineup', 'picture', 'present') 

 isPaused = 0;
 console.log("isPaused at function call ending: " + isPaused);
 })
 .catch(function(err) {
 console.log('Caught an error!', err);
 });

}

Here the output in the console can be seen. The initial "Async success!" prints followed by the output of the object (as returned by the database call), as it should. In addition I see the 'isPaused' variable being changed to "0" when the 'resolved' (.then) statement finishes...as it should...however then everything seems to 'hang'...I do not see any log printouts for my "DATABASE CALL COMPLETED" and the associated arrays that should have populated at that point...

Async success! [ { name: 'administration',
pass: 'peace',
status: 'active',
login: '2090',
logout: 'na',
service: 'both',
lucky: 'na',
picture: null,
picturex: null,
city: null,
state: null,
legacy: 'na' } ]
length of value passed from database query: 1
isPaused at function call ending: 0
//HANGS HERE AT THIS POINT...???

How can my code be modified so as to properly wait for the database call to complete (i.e. '_line_update()' which in turn calls 'db.getMembers')...? Thank you in advance for any suggestions.

Pangit
  • 564
  • 1
  • 7
  • 23
  • Have `_line_update` return the Promise and then `await` it (or call `.then` on it and put everything inside the `.then`) – CertainPerformance Nov 18 '19 at 05:12
  • Thank you for the reply. Not sure what you mean by 'have _line_update return the promise'...? Do you mean directly put the database promise call in that function...? In other words eliminate the 'db.getMembers' call and move '_line_update' into my 'queries' script...? – Pangit Nov 18 '19 at 05:22
  • Your `_line_update` returns a Promise, but you don't do anything with it. You need to return it and consume it outside. – CertainPerformance Nov 18 '19 at 05:22
  • I thought that is what the .then and .catch are doing...acting upon the promise (either 'resolve' or 'reject')...? Sorry I am kinda new to using promises... – Pangit Nov 18 '19 at 05:24
  • Yes, but they're all *inside* the function. Since you want the asynchronous action to be observed and acted on outside, you need to return the Promise. – CertainPerformance Nov 18 '19 at 05:25
  • So I should eliminate the '_line_update()' function entirely...and just place the 'db.getMembers()' call inside the route for '/lookup'...and do all the processing within the '.then' statement at that point...if that is what you are 'saying'? – Pangit Nov 18 '19 at 05:29
  • OK I am playing around with it now...early results seem to be encouraging...however it would then seem it is not possible to 'delay' script execution outside of the route in another function...? What if I had multiple routes that wanted to make the same database call...all the promise processing needs to be handled within each route separately...? – Pangit Nov 18 '19 at 05:45
  • Still not sure if this is useful...I can printout the values of my arrays within the '.then' statement...however if I attempt to do so elsewhere...say at the end of the '/lookup' route...they are 'empty' since the non-blocking nature of node.js simply executes before the promise has been completed...how can I access these arrays elsewhere in my script...??? – Pangit Nov 18 '19 at 05:54
  • *Everything* that depends on the variables being populated should be put inside the `.then`. You can also use `await` instead of `.then` if you want the code to look flatter – CertainPerformance Nov 18 '19 at 06:03
  • Thank you for your responses...I will keep working with it. – Pangit Nov 18 '19 at 06:06

0 Answers0